Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Retrieving latest record from database
6 Posts
Retrieving latest record from database

Though User.last will work in most of the scenarios, but if you have any default_scope on your AR::Base, it will be considered while fetching the last record. Say if you have:

class User < ActiveRecord::Base

default_scope :order => "login DESC"


User.last will return last user sorted by login.

If you just the last entry in the table, you can use this:

User.last Investment.last Comment.last

Thanks guys for ur help........

I would do it like that: a = [] a << User.find(:first, :order =>"created_at DESC") a << Investment.find(:first, :order =>"created_at DESC") a << Comment.find(:first, :order =>"created_at DESC") a..sort {|a,b| a.created_at b.created_at}.first

You can use this: User.all(:order => "id DESC", :limit => 5) And same on your other models.


I have three tables( users, investments, comments).

my reqirement is to display recent entered record from all the three tables.

6 Posts
Login to add your message