Forum: Ruby Find the 3 newest rows in the database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5e197c8f7663929dd8c2d978f0f0d0eb?d=identicon&s=25 david (Guest)
on 2006-05-02 06:33
Hi,

I have an Adverts table with a created_at column. I would like to get
back the 3 newest adverts. How would I do this.

Thanks

David
A402df36168b81b31c17adcbb5ae8cf4?d=identicon&s=25 Pistos Christou (pistos)
on 2006-05-02 06:58
david wrote:
> I have an Adverts table with a created_at column. I would like to get
> back the 3 newest adverts. How would I do this.

This depends on what you're using, but it's a relatively simple SQL
statement:

SELECT *
FROM adverts
ORDER BY created_at DESC
LIMIT 3;

Are you using DBI?  Straight SQL?  ActiveRecord?  Og?

Pistos
5e197c8f7663929dd8c2d978f0f0d0eb?d=identicon&s=25 david (Guest)
on 2006-05-02 07:05
Pistos Christou wrote:
> david wrote:
>> I have an Adverts table with a created_at column. I would like to get
>> back the 3 newest adverts. How would I do this.
>
> This depends on what you're using, but it's a relatively simple SQL
> statement:
>
> SELECT *
> FROM adverts
> ORDER BY created_at DESC
> LIMIT 3;
>
> Are you using DBI?  Straight SQL?  ActiveRecord?  Og?
>
> Pistos

ActiveRecord
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 j`ey (Guest)
on 2006-05-02 08:44
david wrote:
> Pistos Christou wrote:
>> david wrote:
>>> I have an Adverts table with a created_at column. I would like to get
>>> back the 3 newest adverts. How would I do this.
>>
>> This depends on what you're using, but it's a relatively simple SQL
>> statement:
>>
>> SELECT *
>> FROM adverts
>> ORDER BY created_at DESC
>> LIMIT 3;
>>
>> Are you using DBI?  Straight SQL?  ActiveRecord?  Og?
>>
>> Pistos
>
> ActiveRecord

find(:all, :limit => 3, :order => 'created_at DESC')

Check out the Rails mailing list, it would be better to ask these sort
of questions there.

j`ey
http://www.eachmapinject.com
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-05-03 18:59
(Received via mailing list)
use SQL of

SELECT * FROM Adverts ORDER BY created_at DESC LIMIT = 3;
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-05-03 18:59
(Received via mailing list)
Advert.find(:all, :order => "created_at DESC", :limit => 3)
This topic is locked and can not be replied to.