ruby on rails - Loading associated data in a single query -
ruby on rails - Loading associated data in a single query -
that's terrible title, here goes.
i have 2 models, item , category. it's many-to-many relationship (items can belong multiple categories, categories can hold multiple items).
what's best way (e.g. 'name') list of categories , find items included in them? if using sql, i'd following:
select * items category_id in ( select id category name in ('cat1', 'cat2', 'cat3') ) or something, i'd in railsy way if possible.
thanks!
you can do
item.joins(:categories).where("categories.name" => ['cat1', 'cat2', 'cat3']) ruby-on-rails
Comments
Post a Comment