Home Rails: will_paginate with multiple models
Reply: 0

Rails: will_paginate with multiple models

user1428 Published in July 20, 2018, 4:57 am

I am having a really hard time figuring this one out and I'm hoping someone else has had to do something similar before:

I have two models:

class Device < ActiveRecord::Base
  has_many :pm_visits

class PmVisit < ActiveRecord::Base
  # attr_accessible :pm_date
  belongs_to :device

I want to list the devices sorted by the date of the last PM (preventative maintenance) visit and also use will_paginate to limit the number of devices per page.

The partial solution I have so far works in that it shows me the correct results, but doesn't work with will_paginate, and is do inefficient and ugly I am almost ashamed to show it, but here it is (in the devices_controller):

@dev_list = []
dev_ids = PmVisit.joins(:device).where(devices.tech_id: 1).order("pm_date desc").map{|d| d.device_id}.uniq!
dev_ids.each do |id|
  @dev_list << Device.find(id)

Having required 'will_paginate/array' I hoped ...

@dev_list.paginate(page: params[:page])

would paginate the array @dev_list but the will_paginate @dev_list line in the view gives me an error about total_pages method not found for Array object.

I am also really unhappy with how I build @dev_list. If anyone can suggest a better way I would really appreciate it.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.25882 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO