Howdy railsters,
I have a problem which at first seemed to be very simple, but now I'm quite stumped. I'd appreciate your help with this if possible. I've scoured the web and the mailing list for several months now, and I haven't found an acceptable solution. Perhaps you've run into this?
I have two models, associated with a HABTM (actually using has_many :through on both ends, along with a join table). I need to retrieve all ModelA's that is associated with BOTH of two ModelB's. I do NOT want all ModelA's for ModelB_1 concatenated with all ModelA's for ModelB_2. I literally want all ModelA's that are associated with BOTH ModelB_1 and ModelB_2. It is not limited to only 2 ModelB's, it may be up to 50 ModelB's, so this must scale.
I can describe the problem using a variety of analogies, that I think better describes my problem than the previous paragraph:
* Find all books that were written by all 3 authors together. * Find all movies that had the following 4 actors in them. * Find all blog posts that belonged to BOTH the Rails and Ruby categories for each post. * Find all users that had all 5 of the following tags: funny, thirsty, smart, thoughtful, and quick. (silly example!) * Find all people that have worked in both San Francisco AND San Jose AND New York AND Paris in their lifetimes.
I've thought of a variety of ways to accomplish this, but they're grossly inefficient and very frowned upon.
Taking an analogy above, say the last one, you could do something like query for all the people in each city, then find items in each array that exist across each array. That's a minimum of 5 queries, all the data of those queries transfered back to the app, then the app has to intensively compare all 5 arrays to each other (loops galore!). That's nasty, right?
Another possible solution would be to chain the finds on top of each other, which would essentially do the same as above, but won't eliminate the multiple queries and processing. Also, how would you dynamicize the chain if you had user submitted checkboxes or values that could be as high as 50 options? Seems dirty. You'd need a loop. And again, that would intensify the search duration.
Obviously, if possible, we'd like to have the database perform this for us, so, people have suggested to me that I simply put multiple conditions in. Unfortunately, you can only do an OR with HABTM typically.
Another solution I've run across is to use a search engine, like sphinx or UltraSphinx. For my particular situation, I feel this is overkill, and I'd rather avoid it. I still feel there should be a solution that will let a user craft a query for an arbitrary number of ModelB's and find all ModelA's.
How would *you* solve this problem?
Thanks a bunch, Kevin
(I've subsequently cross-posted this to StackOverflow's website since they have a target audience that encompasses more than Rails, and is still valid in those areas --- sql - HABTM finds with "AND" joins, NOT "OR" - Stack Overflow )