ruby on rails - Active Record query based on property of has_many relationship? -
here's common occurrence. have parent category, category
. has_many child categories, books
. books have property published
, , want categories have published books. how do that?
i loop through categories find them, want better way. in this railscast, suggests using following query: category.joins(:products).merge(product.cheap)
, or subject.joins(:books).merge(book.published)
in example. however, don't have scope published
in book
.
i have method all_published
in book
returns published books, tried category.joins(:books).merge(book.all_published)
contained duplicate categories. what's best general way solve common problem?
the .joins
makes duplicates, whereas .includes
not.
in case, following should work:
category.includes(:books).merge(book.all_published)
Comments
Post a Comment