Last record from a has_many association?

I am trying to find a particular (in this case the last, or most
recent) status for a user. As a user does certain things, new statuses
are added (into users_statuses).

I want to be able to show a list of users with their current status. I
get as far as at least getting the full list of users (@users =
User.find(:all)), but not sure how to output the list with just the
most recent status?

Help please?

<<< The models >>>
class User < ActiveRecord::Base
has_many :statuses, :through => :users_statuses
end

class Status < ActiveRecord::Base
has_many :users, :through => :users_statuses
end

class UserStatus < ActiveRecord::Base
belongs_to :user
belongs_to :status
end

<<< The controller >>>
@users = User.find(:all)

hope ur trting to do this
@users = User.find(:all,:include => [:user_statuses], :order=>’
user_statuses.id’)

:slight_smile:
Bala

On 2 Sep 2008, at 10:28, yachtman wrote:

Help please?

If user is an element of the collection then on 2.1,
user.statuses.last. Prior to that, user.statuses.find(:first, :order
=> … ) (user.statuses.last would still work but would load the
entire collection whereas in 2,1 it is smart enough to just load the
last one)

Fred