在Rails中有“嵌套查找”这样的东西吗?

时间:2011-11-30 21:45:49

标签: ruby-on-rails attributes include nested conditional-statements

我已经掌握了嵌套的包含 - 它们对于性能非常有用 - 但我真正喜欢的是'嵌套查找'。实现以下目标的最佳方法是什么:

@matchingProducts = Batch.find(:all,
                               :conditions => 'product.name LIKE ?', "%#{search}%",
                               :include => :product)

如您所见,ProductBatch的嵌套属性,但我希望找到基于Product.name的批处理。

2 个答案:

答案 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)