Bug in paginating_find

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

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

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

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. [email protected]

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, “[email protected][email protected]

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, “[email protected][email protected]

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

Hi Differentthink. Can you post the sql query that is generated by
paginating_find?

On Jun 21, 10:38 am, Differenthink [email protected]

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. [email protected]

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

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. [email protected]

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. [email protected]

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

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 :slight_smile:

On 22 juin, 11:33, Frederick C. [email protected]

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

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

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 :slight_smile:

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