Reputation: 1807
<% @user.friendships.each do |f| %>
<%= f.friend.count %>
<% end %>
This returns a set of numbers 1 2 1 1 3
but when I tried to get the sum of those values , I get this error: undefined method sum for 1:Fixnum
.
<%= f.friend.count.sum %>
<%= f.friend.count.sum(:value) %>
<%= f.friend.count.inject {|sum, x| sum + x} %>
Upvotes: 0
Views: 1839
Reputation: 6419
I think this is what you're looking for:
<%= @user.friendships.inject(0) {|sum, f| sum + f.friend.count} %>
The important thing to note is that you're calling inject on the friendships collection and incrementing the sum by f.count.
Upvotes: 1