I have the following models:
activity.rb
tag.rb
tagging.rb
Tagging is a join model for activity and tag.
I would like to search an Activity that has 2 or more tags. How do I do this in rails?
For example:
I have tag1 = Christmas, tag2 = Florida, tag3 = John
I want to find the Activity where tag1, tag2 and tag3 are present if it exists.
[EDIT]
What I ended up doing:
tags = [tag1, tag2, tag3]
activities = []
tags.each do |tag|
activities << tag.activities
end
activities.flatten.group_by { |activity| activity.id }
If any of the groups values’ size is equal to tags.size then that activity contains all tags.
Not sure but this should be a better performant way to achieve it. But there should be an even better way
This will get activities that have those tags, but they can have others too.