Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

Problem with :include option

#136

I'd like to use will_paginate on a list of Sensor model objects and I'd like to use the :include parameter to pre-fetch some of the objects associated with the Sensor.

So, with regular AR find, I would write:

Sensor.find(:all, :order => "serial_number ASC", :include => [:sensor_type, :most_recent_calibration, :current_location] ).length

That works fine and returns 215. If I sort in DESC order, I also get 215. Good.

But if I do the same thing with paginate, I get odd behavior:

>> Sensor.paginate(:order => "serial_number DESC", :page => 0, :include => [:sensor_type, :most_recent_calibration, :current_location] ).length

=> 28

>> Sensor.paginate(:order => "serial_number ASC", :page => 0, :include => [:sensor_type, :most_recent_calibration, :current_location] ).length

=> 30

Those should both be 30. If I remove the :include option, I get results that make sense:

>> Sensor.paginate(:order => "serial_number ASC", :page => 0 ).length

=> 30

>> Sensor.paginate(:order => "serial_number DESC", :page => 0 ).length

=> 30

What's happening here? I need to be able to use the :include option because in the real application I pass a :conditions parameter that refers to joined tables (e.g. ["sensor_types.type ILIKE ?", '%gauge%'] and sensor_types is not included in the query unless I :include :sensor_type).

Let me know if I can provide more information.

Thanks.

Reported by atkinw (at rpi) · October 19th, 2007 @ 01:52 AM

State: invalid
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. Chris Wanstrath
    Chris Wanstrath
    • Assigned user changed from Chris Wanstrath to Mislav
    • State changed from new to open

    October 19th, 2007 @ 02:07 AM

  2. Mislav
    Mislav

    You can help even more. Go to your project and open up Rails console (script/console).

    Execute this:

    ActiveRecord::Base.logger = Logger.new(STDOUT)
    ActiveRecord::Base.clear_active_connections!
    

    Now perform your stuff that you pasted above. For each "paginate" call perform the "find" equivalent, too. Generated SQL will render in your console; copy and post it here as attachment. Don't forget to indicate was the length expected or not.

    When pasting code in your comment, see "formatting help" at the top of comment input box to learn how you can properly paste.

    October 19th, 2007 @ 02:25 AM

  3. atkinw (at rpi)
    atkinw (at rpi)

    Output attached. I expect 215 sensors to be returned (both of the Sensor.find calls work correctly here) and I expect both of the paginate calls to return the maximum number of sensors per page (30). The call that returns 28 is the problem.

    Here are the interesting parts of the Sensor model:

    class Sensor < SensorBase
      has_many :calibrations, :foreign_key => "associated_sensor", :order => "date DESC"
      has_one :most_recent_calibration, :class_name => "Calibration", :order => "date DESC", :foreign_key => "associated_sensor"
      
      has_many :checkouts
      belongs_to :current_location, :class_name => "Checkout", :foreign_key => "checkout_id"
      
      belongs_to :sensor_type, :foreign_key => "sensor_type"
    
      # ...  
    
      def self.per_page
        30
      end
      
      # ...
    end
    

    October 19th, 2007 @ 02:31 AM

  4. Mislav
    Mislav
    • State changed from open to invalid

    Hey, sorry it took so long. I think the issue is because you're specifying 0 for page number, but 1 is first page. In newer versions of will_paginate (since last Christmas), specifying a page number lower than 1 leads to an error being thrown.

    January 31st, 2008 @ 11:27 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.