Ruby/Rails Collection to Collection

前端 未结 4 1648
天涯浪人
天涯浪人 2021-02-09 23:04

I have a two tables joined with a join table - this is just pseudo code:

Library
Book
LibraryBooks

What I need to do is if i have the id of a l

4条回答
  •  长发绾君心
    2021-02-09 23:21

    One problem with

    l.books.map{|b| b.libraries}.flatten.uniq
    

    is that it will generate one SQL call for each book in l. A better approach (assuming I understand your schema) might be:

    LibraryBook.find(:all, :conditions => ['book_id IN (?)', l.book_ids]).map(&:library_id).uniq
    

提交回复
热议问题