I have a join table that lists a user_id and an item_id (many-to-many relationship).
I'm trying to show how many items a user owns on an erb page.
How do I go about doing this?
My join table/model UserItem has eleven records in right now, so obviously if I do UserItem.count, it gives me 11.
But how can I tell it that for every user_id, count how many item_ids there are for THAT particular user? And then obviously I would iterate over this.
EDIT:
Ok I found that UserItem.group(:user_id).distinct.count gives me back hash of {user_id => # of items per user}.
How do I not only iterate over the hash, but also make it so it associates it with each current user_id?
<% @users.each do |user| %>
<li><%= user.username %>
has been to <%= UserItem.group(:user_id).distinct.count %>!</li>
<% end %>
As of now, this just inserts the hash, how do I correspond each user_id so it aligns with the user.username it's showing?