Ruby/Rails Collection to Collection

前端 未结 4 1649
天涯浪人
天涯浪人 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:05

    Perhaps:

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

    or

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

    if you want it all in a flat array.

    Of course, you should really define this as a method on Library, so as to uphold the noble cause of encapsulation.

提交回复
热议问题