#136 ✓invalid
atkinw (at rpi)

Problem with :include option

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

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.


Comments and changes to this ticket

  • Chris Wanstrath

    Chris Wanstrath October 19th, 2007 @ 02:07 AM

    • Assigned user changed from “Chris Wanstrath” to “Mislav”
    • State changed from “new” to “open”
  • Mislav

    Mislav October 19th, 2007 @ 02:25 AM

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

    Execute this:

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

    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.

  • atkinw (at rpi)

    atkinw (at rpi) October 19th, 2007 @ 02:31 AM

    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
      # ...
  • Mislav

    Mislav January 31st, 2008 @ 11:27 PM

    • 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.

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

People watching this ticket