我已经掌握了嵌套的包含 - 它们对于性能非常有用 - 但我真正喜欢的是'嵌套查找'。实现以下目标的最佳方法是什么:
@matchingProducts = Batch.find(:all,
:conditions => 'product.name LIKE ?', "%#{search}%",
:include => :product)
如您所见,Product
是Batch
的嵌套属性,但我希望找到基于Product.name
的批处理。
答案 0 :(得分:2)
Rails 3我会使用AREL语法:
@matches = Batch.where('product.name LIKE ?', "search").includes(:product)
答案 1 :(得分:0)
你的想法是对的,你可以做matchingProducts = Batch.find(:all, :include => 'products', :conditions => ["products.name LIKE ?", whatever_you_want], :order => order_as_you_want)