# Bug in paginating\_find

**URL:** <https://www.ruby-forum.com/t/bug-in-paginating-find/141051>\
**Category:** Rails\
**Created:** [June 21, 2008, 10:54am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051 "2008-06-21T10:54:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 21, 2008, 10:54am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/1 "2008-06-21T10:54:08Z")

</div>

Hi,

I juste found a bug in the plugin paginating\_find.  
I order my records using an associated model column.

When i do that, record are beign sorted only based on those displayed  
on the page, and not based on all of the records found.

Is anyone experienced this problem ?  
does anyone see a solution ?

Thanks

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 21, 2008, 1:55pm UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/2 "2008-06-21T13:55:07Z")

</div>

Differenthink wrote:

> Hi,
> 
> I juste found a bug in the plugin paginating\_find.  
> I order my records using an associated model column.
> 
> When i do that, record are beign sorted only based on those displayed  
> on the page, and not based on all of the records found.
> 
> Is anyone experienced this problem ?  
> does anyone see a solution ?
> 
> Thanks

Hi Differentthink,

Will you please post the related code? I had a problem not too long ago  
with try to sort on a secondary association, and I thought was a bug in  
associations, but it turned out to be a lack of understand on my part.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 21, 2008, 1:59pm UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/3 "2008-06-21T13:59:42Z")

</div>

Phillip K. wrote:

> Hi Differentthink,
> 
> Will you please post the related code? I had a problem not too long ago  
> with try to sort on a secondary association, and I thought was a bug in  
> associations, but it turned out to be a lack of understand on my part.
> 
> Peace,  
> Phillip

Whew, you’d think that I never spoke/wrote English before.

First, I misspelled your name. Too many tees. Differenthink. Sorry.

Second, “try” should be “trying” in the second sentence.

Third, “…and I thought _it_ was a bug…”

Last (I think), “…lack of understand\_ing\_ on my part.”

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 21, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/4 "2008-06-21T17:38:55Z")

</div>

No problem.

here is my piece of code :

@question = Question.find(:all, :conditions=\>[“systeme\_id IN (?)”,  
@systeme], :include=\> :reponses, :order=\>“reponses.date DESC”, :page  
=\>{ :size=\>10, :current=\>params[:page]})

My :order condition works, but only for records shown on the page by  
pagainting\_find… it s why i guess for a bug in the plugin.  
(when i order by a common column of the record, there is no problem)

Any idea ?

On 21 juin, 13:59, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 21, 2008, 11:21pm UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/5 "2008-06-21T23:21:30Z")

</div>

I ve a big clue !

the problem is not in the plugin, but in my order condition.  
Let me explain.

For each “question”, i can have many “reponse”.  
When i use : “reponses.date DESC” i want to order by the last  
“reponse”… and it seems it use the first one…

Any idea to make it understand to order by last “reponse” ?

Thanks a lot for everything,

On 21 juin, 19:57, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 12:25am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/6 "2008-06-22T00:25:05Z")

</div>

Differenthink wrote:

> I ve a big clue !
> 
> the problem is not in the plugin, but in my order condition.  
> Let me explain.
> 
> For each “question”, i can have many “reponse”.  
> When i use : “reponses.date DESC” i want to order by the last  
> “reponse”… and it seems it use the first one…
> 
> Any idea to make it understand to order by last “reponse” ?
> 
> Thanks a lot for everything,
> 
> On 21 juin, 19:57, “[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

I’m not sure I completely understand what it is you’re trying to do. I  
_think_ you’re looking for two levels of sorting: question then  
response. What field on questions is a reasonable candidate for  
sorting? Name, description, order, id? Suppose you have a field on  
questions called sort\_order, you could do something like

:order =\> ‘questions.sort\_order asc, responses.date desc’

If that’s not what you’re asking, either write me off as loony or  
explain it a bit more clearly.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 21, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/7 "2008-06-21T19:58:52Z")

</div>

Hi Differentthink. Can you post the sql query that is generated by  
paginating\_find?

On Jun 21, 10:38 am, Differenthink [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 12:34am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/8 "2008-06-22T00:34:00Z")

</div>

I want to order by the last “reponse” datetime.  
which should be : “responses.date desc”

but when doing that, it seems that it sorts by the first “reponse”  
found for a question and note the last “reponse” posted…

is it a bit clearer ?  
(think for it as a forum, where there is one “question” and could be  
many “answers” (=reponses)

On 22 juin, 00:25, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 12:55am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/9 "2008-06-22T00:55:03Z")

</div>

On 21 Jun 2008, at 23:33, Differenthink wrote:

> I want to order by the last “reponse” datetime.  
> which should be : “responses.date desc”
> 
> but when doing that, it seems that it sorts by the first “reponse”  
> found for a question and note the last “reponse” posted…

so you want to order questions by the max of responses.date for each  
question ?

Fred

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 11:31am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/10 "2008-06-22T11:31:17Z")

</div>

Yes, this is exactly what i want to do :  
“If you want to order the questions by the _most  
recent response_ for each question”

I ll give a try to your piece of code

thanks

On 22 juin, 00:54, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 12:54am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/11 "2008-06-22T00:54:17Z")

</div>

Differenthink wrote:

> I want to order by the last “reponse” datetime.  
> which should be : “responses.date desc”
> 
> but when doing that, it seems that it sorts by the first “reponse”  
> found for a question and note the last “reponse” posted…
> 
> is it a bit clearer ?  
> (think for it as a forum, where there is one “question” and could be  
> many “answers” (=reponses)
> 
> On 22 juin, 00:25, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Are you trying to display a list of questions according to which one has  
the most recent response? The finder that you posted earlier

@question = Question.find(:all, :conditions=\>[“systeme\_id IN (?)”,  
@systeme], :include=\> :reponses, :order=\>“reponses.date DESC”, :page  
=\>{ :size=\>10, :current=\>params[:page]})

is looking for all questions for the given systeme\_ids, and will join to  
the responses table on each question. So your order clause is going to  
include all the multiple responses for each question. I’m not sure  
that’s what you want. If you want to order the questions by the _most  
recent response_ for each question, you will need to create an  
association such as

class ForumPost \< ActiveRecord::Base  
has\_one :most\_recent\_response, :class\_name =\> ‘Response’, :foreign\_key  
=\> ‘question\_id’, :order =\> ‘date desc’  
end

Then in the finder, include that association

Question.find(:all, :include =\> :most\_recent\_response, :order =\>  
‘responses.date desc’)

Something like that anyway. I’m using some code I wrote earlier this  
year using ForumPosts as a guide, but in my case, the most recent reply  
is also a ForumPost, so my association is a little more complicated. But  
I did something similar.

Oh, now that I look more at my code, I see that I constructed custom SQL  
to do the finding of replies, so this may not work like I think. Give  
it a try, though, and see what happens.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 11:47am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/12 "2008-06-22T11:47:10Z")

</div>

Hum… Ok so there is no good, nice, proper way to do this…

I think of adding a column “date\_last\_rep” on my 'question" model that  
i update each time there is an answer…  
that way it will be easier to sort it 🙂

On 22 juin, 11:33, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/13 "2008-06-22T14:20:50Z")

</div>

Frederick C. wrote:

> including has\_ones like that doesn’t really work (because of  
> how :include works under the scenes. there’s not a nice way in sql (at  
> least not a portable one) to say join this table but give me only the  
> top row and things like that.

You are, of course, correct again, Fred. That would explain why I  
created a custom SQL statement to do my searching.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 11:34am UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/14 "2008-06-22T11:34:15Z")

</div>

On 21 Jun 2008, at 23:54, Phillip K. wrote:

> class ForumPost \< ActiveRecord::Base  
> has\_one :most\_recent\_response, :class\_name =\>  
> ‘Response’, :foreign\_key  
> =\> ‘question\_id’, :order =\> ‘date desc’  
> end

including has\_ones like that doesn’t really work (because of  
how :include works under the scenes. there’s not a nice way in sql (at  
least not a portable one) to say join this table but give me only the  
top row and things like that.  
One way round it would be to do something like  
select distinct questions.id from questions  
inner join responses on question\_id = questions.id  
order by responses.date desc  
limit x  
(i’ll leave it up to you to write this in AR speak)

this gives you the x question ids with most recent responses. You can  
then load those questions & responses.

Fred

---

<div class="post-metadata">

**Author:** ![differenthink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6bbea6/32.png) [@differenthink](https://www.ruby-forum.com/u/differenthink)\
**Post date:** [June 22, 2008, 2:27pm UTC](https://www.ruby-forum.com/t/bug-in-paginating-find/141051/15 "2008-06-22T14:27:03Z")

</div>

Differenthink wrote:

> Hum… Ok so there is no good, nice, proper way to do this…
> 
> I think of adding a column “date\_last\_rep” on my 'question" model that  
> i update each time there is an answer…  
> that way it will be easier to sort it 🙂

I was about to fall asleep last night when I wrote that last post and  
didn’t look at my code well enough. You must be trying to tackle almost  
the same problem I did, because I ended up putting a thread\_modified\_at  
date on what I call the “thread head”. As you plan to do, my  
thread\_updated\_at was stamped whenever a descendent post was added or  
modified. As you can surmise from my statements, my posts are actually  
threaded, not just a post with many responses. The threading makes it a  
bit more difficult in that every time a post is saved, I have to  
traverse the ancestry path backwards to find the head of the thread.  
But, when paginating through the forum’s thread heads, I don’t have to  
do a join to sort them in the order of last update, which was the point.

Peace,  
Phillip
