Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.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 两个表上的Rails where子句_Ruby On Rails_Ruby_Where Clause - Fatal编程技术网

Ruby on rails 两个表上的Rails where子句

Ruby on rails 两个表上的Rails where子句,ruby-on-rails,ruby,where-clause,Ruby On Rails,Ruby,Where Clause,我在rails应用程序中有以下模型 category => company => store 商店有一个属于公司,公司有一个属于类别关系。 现在我想对store对象使用where方法来检索同一类别中的所有商店 我想要这样的 @stores.nearbys(5).where("stores.company.category_id = xxx") 有人能给我一个提示吗其中支持嵌套哈希 @stores.nearbys(5).where(:company => { :categor

我在rails应用程序中有以下模型

category => company => store
商店有一个
属于
公司,公司有一个
属于
类别关系。 现在我想对store对象使用where方法来检索同一类别中的所有商店

我想要这样的

@stores.nearbys(5).where("stores.company.category_id = xxx")

有人能给我一个提示吗

其中
支持嵌套哈希

@stores.nearbys(5).where(:company => { :category_id => @category.id }, joins: company)
你可以试试这个

 @category = Category.find xxx

 @store = @category.company.stores

尝试使用联接表上的where进行联接:

@stores.nearbys(5).joins(:company).where("companies.category_id = xxx")
编辑:

要获取商店的类别,首先必须将category方法委托给其公司:

class Store < ActiveRecord::Base
 belongs_to :company

 delegate :category, :to => :company
end

你好,simone,有@store.nearbys(5)。where(company:{category_id},joins:company)我得到了以下错误-语法错误,意外的'}',期待tASSOC…(company:{category_id},joins:company)也许你没有使用Ruby 1.9?我更新为使用旧语法。抱歉,出现错误。我忘记了
category\u id
必须有一个值,在这种情况下
@category.id
(确保修改代码)erez的回答解决了我的问题,但仅以您博客的背景为例谢谢!奥地利(维也纳)最好的情况是,在这些情况下,你能重复使用一些命名的作用域吗?这不是一个真正的选项,因为我有一个现有的store对象,我想得到属于同一类别的所有商店…如果可能的话,我们能有另一种关系吗,就像在stores中属于:category,:through=>:companygreat!还有一个问题。我如何用商店的实际类别替换xxx,因为它也可以通过相关公司访问??谢谢。和simone的博客一起解决了我的问题。祝您在奥地利一切顺利
@stores.nearbys(5).joins(:company).where("companies.category_id = ?", self.company.id)