#250 new
Adam Florin

Will_paginate mangles object ids if named_scope includes a :join

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

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.

Comments and changes to this ticket

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 ยป

Shared Ticket Bins

People watching this ticket

Pages