Going on from the user -> member -> team, model I''ve been working on, one thing I''d like to have in my Team class is the ability to return how many users are members of a team. Something like... <%= Self.Current_User.Member.Member_Count %> Maybe I could do this by putting in a function in the helper or model of Members, E.g. def Member_Count(team_id) @members = Member.Find(:all, :condition => ['' team_id = ?'', team_id]) return @members.count end Would this work? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
or better still.... def Member_Count @members = Member.Find(:all, :condition => ['' team_id = ?'', self.team_id]) return @members.count end ? assuming the team_id is held within the relevant member record. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi John, i am not sure i understood what you are trying to do, but if you want to display a count value of an association, i suggest you would use a cache_counter column (google it, it''s there) instead of querying the whole thing. On Dec 19, 1:28 pm, John Griffiths <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> or better still.... > > def Member_Count > @members = Member.Find(:all, :condition => ['' team_id = ?'', > self.team_id]) > return @members.count > end > > ? > > assuming the team_id is held within the relevant member record. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 19 Dec 2007, at 11:13, John Griffiths wrote:> > Going on from the user -> member -> team, model I''ve been working on, > one thing I''d like to have in my Team class is the ability to return > how > many users are members of a team. > > Something like... > > <%= Self.Current_User.Member.Member_Count %> > > Maybe I could do this by putting in a function in the helper or > model of > Members, >if you use associations, you get a count method for free on associations so if team has_many :members then you can do team.members.count (which unlike your example won''t load the association if it doesn''t need to) Fred> E.g. > > def Member_Count(team_id) > @members = Member.Find(:all, :condition => ['' team_id = ?'', > team_id]) > return @members.count > end > > Would this work? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
thanks for that, i thought that might work but didn''t have the guts to try it, <%= @user.member.team.members.count %> worked fine. i''ve got the associations setup right so big benefit. Thanks Fred and Elad Merry Christmas, John. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---