0
私はモデルがCollection
、Product
、Gallery
の3モデルです。すべての製品にギャラリーがあるわけではありません。どのようにそれらを見つけるのですか?モデルにネストされたモデルの接続があるかどうかをチェック
Collection.find_each do |collection|
collection.products.each do |product|
next if collection.products.empty?
puts "Product #{product.id} does not have gallery" unless product.galleries.present?
end
end
それは、クエリのトンを送るので、これは、悪い方法である:
これは私がして来たものです。それをどうすれば改善できますか?
Upd。
class Collection
has_many :products
end
class Product
belongs_to :collection
has_many :galleries
end
class Gallery
belongs_to :product
end
わからないのこのラインを使用しますが、多分これ 'Collection.includes(製品のようなものができ(ギャラリー:{id:nil}) ' – Mtihc
質問を編集してモデル間の関係を追加できますか? – Jeremie
おそらく 'Product.includes(:galleries).where(gallery:{id:nil})'のようなものでしょうか? – Mtihc