# Dynamic Values in Team Model?

**URL:** <https://www.ruby-forum.com/t/dynamic-values-in-team-model/123640>\
**Category:** Rails\
**Created:** [December 19, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/dynamic-values-in-team-model/123640 "2007-12-19T12:13:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 19, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/dynamic-values-in-team-model/123640/1 "2007-12-19T12:13:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 19, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/dynamic-values-in-team-model/123640/2 "2007-12-19T12:28:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 19, 2007, 12:47pm UTC](https://www.ruby-forum.com/t/dynamic-values-in-team-model/123640/3 "2007-12-19T12:47:13Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 19, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/dynamic-values-in-team-model/123640/4 "2007-12-19T13:39:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [December 19, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/dynamic-values-in-team-model/123640/5 "2007-12-19T15:14:10Z")

</div>

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.
