I have a Rails application, I have the following DB schema:
each User has many Members and each Member has many Tags.
The link between the tables is direct (Members table has a user_id column and Tags has a member_id column).
What is the most efficient way to count the number of Tags associated with the user?
Is it through Rails and activerecord or pure sql?
Any thoughts/examples?
In your Tag model you can add a scope:
scope :count_by_user, lambda { |user|
members.where(“members.user_id >= ?”, user)
}
How about that? Do you have the members function/relationship in your Tag model?