Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 有没有a';嵌套查找';在轨道上?_Ruby On Rails_Attributes_Include_Nested_Conditional Statements - Fatal编程技术网

Ruby on rails 有没有a';嵌套查找';在轨道上?

Ruby on rails 有没有a';嵌套查找';在轨道上?,ruby-on-rails,attributes,include,nested,conditional-statements,Ruby On Rails,Attributes,Include,Nested,Conditional Statements,我已经掌握了嵌套包含的窍门,它们对性能非常好,但我真正想要的是“嵌套查找”。实现以下目标的最佳方式是什么: @matchingProducts = Batch.find(:all, :conditions => 'product.name LIKE ?', "%#{search}%", :include => :product) 如您所见,Product是Ba

我已经掌握了嵌套包含的窍门,它们对性能非常好,但我真正想要的是“嵌套查找”。实现以下目标的最佳方式是什么:

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

如您所见,
Product
Batch
的一个嵌套属性,但我想找到一个基于
Product.name

的批。您的想法是对的,您可以做
matchingProducts=Batch.find(:all,:include=>'products',:conditions=>[“products.name LIKE?”,随便您想要什么],:order=>按您想要的顺序订购)

Rails 3我会使用AREL语法:

@matches = Batch.where('product.name LIKE ?', "search").includes(:product)

谢谢你的回复,我已经根据你的例子修改了我的代码,但是运气不好。请您澄清一下,您的代码与我的代码有什么显著区别?是使用“products”而不是:product还是其他什么?我使用了表名“products”而不是符号:product,然后在sql查询中将其用作条件。这就是区别。上面的代码适合我。执行查找时,您会遇到什么错误?感谢您提供的额外信息,非常感谢。我得到的错误是,“找不到名为‘products’的关联;可能是您拼错了?”,但这正是我来呈现结果的时候(顺便说一句,我也尝试将其与will_paginate结合起来,但希望简化我的原始问题)。谢谢,这个回复帮我解决了问题,非常感谢。