#270 ✓invalid
Brian

:include brings back more records then I specify per page

Reported by Brian | May 27th, 2009 @ 10:35 PM

If I call paginate on a Model.all with an :include specified, the query that brings back the included records is pulling back all the records instead of only the included records for the current page. Below is an example:

MspLocation.all(:include => [{:msp_profile => [:msp_services]}], :joins => [:msp_profile], :conditions => ["country = ?", country], :order => "company_name").paginate(:page => 1)
** I have the page size set to 30

the previous command generates the following log output

MspLocation Load (3.2ms) SELECT "msp_locations".* FROM "msp_locations" INNER JOIN "msp_profiles" ON "msp_profiles".id = "msp_locations".msp_profile_id WHERE (country = E'United States') ORDER BY company_name

MspProfile Load (1.2ms) SELECT * FROM "msp_profiles" WHERE ("msp_profiles"."id" IN (22,66,55,44,33,11,23,56,45,34,7,67,24,57,46,35,8,68,25,58,47,36,70,69,60,59,48,37,26,71,61,50,49,38,16,27,62,51,40,39,28,17,63,52,41,18,30,29,64,20,53,19,42,31,21,65,54,43,32,5))

As you can see, the second query is bringing back more then 30 records. I would expect it to only bring back the records the view needs to display for the current page.

Comments and changes to this ticket

  • Mislav

    Mislav May 27th, 2009 @ 11:09 PM

    • State changed from “new” to “invalid”

    It's because you're basically writing this:

    MspLocation.all(...).paginate(...)
    

    You're first loading all the records, then paginating that array. Wrong? Yes.

    The right way:

    MspLocation.paginate(:page => 1,
      :include => {:msp_profile => :msp_services},
      :joins => :msp_profile,
      :conditions => {:country => country}, :order => "company_name"
    )
    

    I don't think you need the ":joins" clause, too.

  • Brian

    Brian May 27th, 2009 @ 11:13 PM

    Thanks for the quick response. That makes sense. I'll update my code.

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

Tags

Pages