Marnen Laibow-Koser wrote:
> [...]
>>> [outer SELECT and JOIN as above]
>>> ORDER BY article.id, version.created_at desc
>>> GROUP BY article.id
>>> [further JOINs may be necessary in some DBs, but the principle holds]
>> 
>> How does this ensure that I get the latest version? 
> 
> Sorry, I meant versions.updated_at desc in the order clause, not 
> created_at.  And the way it works is this: the ORDER clause sorts the 
> versions by article ID, then by timestamp, ensuring that versions for 
> the same article are together with the newest first. Then the GROUP 
> clause returns only one record -- the first, ergo the newest -- for each 
> article ID.

Well, you learn something new every day!  I found the Ambiguous Groups 
article on the Web and found that the behavior I thought was standard -- 
returning the first value in the case of multiple values -- is actually 
a MySQL quirk (ironic when you consider that I haven't used MySQL in 
years).  Back to the drawing board.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

Sent from my iPhone
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to