<?xml version="1.0" encoding="UTF-8"?>
<tickets type="array">
  <ticket>
    <assigned-user-id type="integer">51</assigned-user-id>
    <attachments-count type="integer">0</attachments-count>
    <closed type="boolean">false</closed>
    <created-at type="datetime">2009-02-23T11:10:22-08:00</created-at>
    <creator-id type="integer">25969</creator-id>
    <milestone-due-on type="datetime" nil="true"></milestone-due-on>
    <milestone-id type="integer" nil="true"></milestone-id>
    <number type="integer">250</number>
    <permalink>will_paginate-mangles-object-ids-if-named_scope-includes-a-join</permalink>
    <priority type="integer">101</priority>
    <project-id type="integer">466</project-id>
    <raw-data type="binary" nil="true" encoding="base64"></raw-data>
    <state>new</state>
    <tag>will_paginate</tag>
    <title>Will_paginate mangles object ids if named_scope includes a :join</title>
    <updated-at type="datetime">2009-02-23T11:16:23-08:00</updated-at>
    <user-id type="integer">25969</user-id>
    <user-name>Adam Florin</user-name>
    <creator-name>Adam Florin</creator-name>
    <assigned-user-name>Chris Wanstrath</assigned-user-name>
    <url>http://err.lighthouseapp.com/projects/466/tickets/250</url>
    <original-body>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 &lt; ActiveRecord::Base
  named_scope :visible, :conditions =&gt; {:status =&gt; 'visible'}
end
@@@

and then do:

@@@
Review.visible.paginate(:page =&gt; 1)
@@@

All the returned reviews are OK.

However, if I just add a join to that named_scope (perfectly legal) like:

@@@
class Review &lt; ActiveRecord::Base
  named_scope :visible, :conditions =&gt; {'reviews.status' =&gt; ['list', 'feature']},
    :joins =&gt; &quot;JOIN stories ON reviews.story_id=stories.id&quot;
end
@@@

...and then run the same paginate call as above, my results look like:

@@@
[#&lt;Review id: 182, story_id: 182&gt;, #&lt;Review id: 183, story_id: 183&gt;, ...]
@@@

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 =&gt; &quot;reviews.id, reviews.story_id&quot;, then the ids _are_ correct.</original-body>
    <latest-body>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 &lt; ActiveRecord::Base
  named_scope :visible, :conditions =&gt; {:status =&gt; 'visible'}
end
@@@

and then do:

@@@
Review.visible.paginate(:page =&gt; 1)
@@@

All the returned reviews are OK.

However, if I just add a join to that named_scope (perfectly legal) like:

@@@
class Review &lt; ActiveRecord::Base
  named_scope :visible, :conditions =&gt; {'reviews.status' =&gt; ['list', 'feature']},
    :joins =&gt; &quot;JOIN stories ON reviews.story_id=stories.id&quot;
end
@@@

...and then run the same paginate call as above, my results look like:

@@@
[#&lt;Review id: 182, story_id: 182&gt;, #&lt;Review id: 183, story_id: 183&gt;, ...]
@@@

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 =&gt; &quot;reviews.id, reviews.story_id&quot;, then the ids _are_ correct.</latest-body>
    <original-body-html>&lt;div&gt;&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Review &amp;lt; ActiveRecord::Base
  named_scope :visible, :conditions =&amp;gt; {:status =&amp;gt; 'visible'}
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then do:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
Review.visible.paginate(:page =&amp;gt; 1)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All the returned reviews are OK.&lt;/p&gt;
&lt;p&gt;However, if I just add a join to that named_scope (perfectly
legal) like:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Review &amp;lt; ActiveRecord::Base
  named_scope :visible, :conditions =&amp;gt; {'reviews.status' =&amp;gt; ['list', 'feature']},
    :joins =&amp;gt; &amp;quot;JOIN stories ON reviews.story_id=stories.id&amp;quot;
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;...and then run the same paginate call as above, my results look
like:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
[#&amp;lt;Review id: 182, story_id: 182&amp;gt;, #&amp;lt;Review id: 183, story_id: 183&amp;gt;, ...]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where the story_ids are all correct and the Review ids are all
incorrectly set to match the story ids.&lt;/p&gt;
&lt;p&gt;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 =&amp;gt; &quot;reviews.id,
reviews.story_id&quot;, then the ids &lt;em&gt;are&lt;/em&gt; correct.&lt;/p&gt;&lt;/div&gt;</original-body-html>
  </ticket>
</tickets>
