I have the parent model of user
and user
has_many :events
. From the user
view, how can I find the most recent datetime (event.time
) of event
?
I'm thinking that a find_by will work, but I'm not sure on how to do that.
I have the parent model of user
and user
has_many :events
. From the user
view, how can I find the most recent datetime (event.time
) of event
?
I'm thinking that a find_by will work, but I'm not sure on how to do that.
Something like this.
user.events.find(:first, :order => "time DESC")
You can read more here:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001777
user.events.find(:all, :order => 'time desc', :limit => 100)
where limit is number of recent events you need or:
user.events.find(:first, :order => 'time desc')
if you need one most recent event.
user.events.find(:first, :order => 'time desc').time
like this you can get the event time