class Event
has_many :keywords, :through => :event_keywords
end
class User
has_many :keywords, :through => :user_keywords
end
I have a User method that calculates an event's relevance based on the keywords it shares with a specific user:
User.rb
def calculate_event_relevance(event_id)
event_keywords = event.keywords
event_keywords.each do |kw|
user_keyword_match = self.keywords.where(id: kw.id).first
if user_keyword_match
## do some stuff
end
end
end
Right now, I am looping through each keyword. In the loop, I make a query to see if the user also has that keyword.
Is there a way instead to make one single query (and save it to event_keywords) that only returns the event.keywords that a user also has so that I don't need to loop through all of them?
Is there a query that can find which keywords a user and event have in common?