0
votes

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.

1

1 Answers

3
votes

Your Books model will have the following indexes defined (apart from others that you may chose):

define_index do
  ....
  ....
  indexes title
  has owner_ids
end

With this index, assuming you have the target 'user', the search command will become:

Book.search conditions: {title: 'Lorem'}, with: {owner_ids: user.friend_ids}