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?
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.
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 G. [email protected]
On 19 Dec 2007, at 11:13, John G. wrote:
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
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.