ruby on rails - Finding records with two specific records in another table -


i have product model has_and_belongs_to_many :taxons, , want find products in specific taxons.

for example, if product belongs both "ruby on rails" , "shirts" taxon, want product returned in dataset, not if belongs either "ruby on rails" or "shirts"

i had problem while back, thankfully there nice solution.

def self.has_taxons(taxons)   id = arel_table[:id]   product.joins(:taxons).where(taxons: { name: taxons }).group(id).having(id.count.eq(taxons.size)) end 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -