Reputation: 5073
I have a table user_keywords which :belongs_to :keywords. user_keywords has a column keyword_id, user_id, and relevance_score (float).
keyword table has a column 'name'.
What is the most efficient way to query this?
Upvotes: 0
Views: 678
Reputation: 4615
try this:
Keyword.joins(:user_keywords)
.select('keywords.name, avg(user_keywords.relevance_score) as score')
.group('keywords.name')
.order('score DESC')
.map(&:name)
Upvotes: 1