Maybe such problem was already discussed here, but I can't even realize, how to properly google such case.
In Rails I have following models:
User (has_many book_specimens, has_many friendships, has_many friends through friendship, has_many books through book_specimens)
Book_specimen (belongs_to user, belongs_to book)
Book (has_many book_specimens, has_many owners through book_specimens)
Friendship (belongs_to user, belongs_to friend)
What I need is to search books amongs those which user's friends have. If breaking into not-sql logic, it would look like
results = [];
friends.each do |friend|
results.push(Book.search conditions: {title: 'Lorem', owner_id: friend.id})
end
Is there any way I can do it in one command? How should I prepare indices then?
Thanks in advance.