Discussion Forums
- Topic List
- Most Recent Posts
- Sign In for more options
hi,
I have three tables( users, investments, comments).
my reqirement is to display recent entered record from all the three tables.
You can use this: User.all(:order => "id DESC", :limit => 5) And same on your other models.
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
If you just the last entry in the table, you can use this:
User.last Investment.last Comment.last
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"
end
User.last will return last user sorted by login.
