The reason your first try didn’t work is you’re passing a string that
isn’t going to be parsed by #find, when what you need is an array.
This is closer to the correct syntax:
So now you have three arrays of newsletters that have each of the IDs.
Then, run a set intersection on them:
That is the logic I’d normally start with for something like this.
Fortunately, there are some useful plugins which can solve this sort of
problem for you. One is squirrel:
To do the intersection in SQL, you need to use a HAVING clause to match
the number of entries with the number of items checked against. In this
case, using squirrel:
required_tags = [12, 16]
amount = required_tags.length
items = Newsletter.find(:all,
:group => “newsletters.id HAVING count(*) = #{amount}”)
do
tags.id === required_tags
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.