Finding records with two specific records in another table

前端 未结 3 1993
再見小時候
再見小時候 2021-02-09 13:19

I have a Product model that has_and_belongs_to_many :taxons, and I want to find all products that are in specific taxons.

For example, if a pro

3条回答
  •  故里飘歌
    2021-02-09 14:15

    Assuming performance isn't a requirement:

    a = Taxon.find_by_name!('Ruby on Rails').products.pluck(:id)
    b = Taxon.find_by_name!('Shirts').products.where(:id => a)
    

提交回复
热议问题