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.

Will_paginate mangles object ids if named_scope includes a :join

#250

It looks like if you use will_paginate in conjunction with an ActiveRecord named_scope with a :join, will_paginate will jam the ids of the joined table in place of the actual ids of the returned objects. Kinda hard to explain; it's a weird bug.

I'll pull an example from our source. Take two models, Review and Story, where Review belongs_to :story. If I set up a simple named scope like:


class Review < ActiveRecord::Base
  named_scope :visible, :conditions => {:status => 'visible'}
end

and then do:


Review.visible.paginate(:page => 1)

All the returned reviews are OK.

However, if I just add a join to that named_scope (perfectly legal) like:


class Review < ActiveRecord::Base
  named_scope :visible, :conditions => {:status => 'visible'},
    :joins => "JOIN stories ON reviews.story_id=stories.id"
end

...and then run the same paginate call as above, my results look like:


[#<Review id: 182, story_id: 182>, #<Review id: 183, story_id: 183>, ...]

Where the story_ids are all correct and the Review ids are all incorrectly set to match the story ids.

This has to do with how will_paginate or something is parsing the SQL results (which are correct). Note that if I feed an explicit :select to paginate like :select => "reviews.id, reviews.story_id", then the ids are correct.

Reported by Adam Florin · February 23rd, 2009 @ 11:10 AM

State: new
Milestone: none
Assigned to: Chris Wanstrath Chris Wanstrath

Activity

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.