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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
bala kishore pulicherla
2008-Sep-02 09:39 UTC
Re: Last record from a has_many association?
hope ur trting to do this @users = User.find(:all,:include => [:user_statuses], :order=>'' user_statuses.id'') :) Bala On Tue, Sep 2, 2008 at 2:58 PM, yachtman <carson.cole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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) > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2 Sep 2008, at 10:28, yachtman wrote:> > 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? >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> > <<< 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) > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---