Reputation: 3522
I am attempting to count the amount of users with a certain team_id allocated to them.
at present we have a users table and a teams table, our users table has a belongs_to
our teams table and the teams table has many-to-many relationship with the user.
I have the following code in my teams_helper
def number_of_players(team)
User.count("team_id", :conditions => team_id= :team)
end
And I am calling this in my view:
%td= number_of_players(team.id)
The problem I'm having is that is isn't counting correctly.
Upvotes: 1
Views: 89
Reputation: 4394
How about that?
# if team is an integer
def number_of_players(team)
User.where(:team_id => team).count
end
Or
# if team is an instance of Team and `has_many :users`
def number_of_players(team)
team.users.count
end
Upvotes: 1