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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.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 具有嵌套关联的Rails作用域_Ruby_Ruby On Rails 4_Devise_Scopes - Fatal编程技术网

Ruby 具有嵌套关联的Rails作用域

Ruby 具有嵌套关联的Rails作用域,ruby,ruby-on-rails-4,devise,scopes,Ruby,Ruby On Rails 4,Devise,Scopes,我正在努力掌握rails的作用域。我有简单的基础知识,但我试图创建一个稍微复杂一点的范围,我遇到了一些麻烦 class Client has_many :referrals, through: :submissions has_one :address has_many :submissions end class Submission belongs_to :client belongs_to :user has_many :referrals, :inv

我正在努力掌握rails的作用域。我有简单的基础知识,但我试图创建一个稍微复杂一点的范围,我遇到了一些麻烦

class Client
  has_many :referrals, through: :submissions
  has_one :address
  has_many :submissions
end

class Submission
    belongs_to :client
    belongs_to :user
    has_many :referrals, :inverse_of => :submission, dependent: :delete_all
end

class Referral
    belongs_to :branch
    belongs_to :submission
    has_one :client, through: :submission
end

class Address
    belongs_to :client
end
我也有使用Desive创建的用户。我为用户添加了一个名为city_town的自定义属性

当用户注册时,他们会选择他们来自哪个城市或城镇以及为哪个机构工作。创建提交时,它们会为客户端详细信息和地址以及引用详细信息采用嵌套属性。推荐采用指定推荐目的地的机构id

我试图实现的是创建一个范围,该范围将收集referral.client.address.city\u town与当前用户的城市或城镇匹配的所有转介,即:current\u user.city\u town和转介的代理id与登录用户的代理id匹配

简而言之,当用户登录时,他们只能看到他们所在机构和地区的推荐

到目前为止,我已经:

class Referral
  scope :agency_referrals, -> (id, city_town) { includes(:clients).where(agency_id: id, client.address.city_town => city_town) }
end
但我痛苦地意识到这远远不是正确的。我得到这个错误:

undefined local variable or method `client' for #<Class:0x00000003200c08>
知道我哪里出错了吗?

你能试试吗

includes(submission: :clients)
你能试试吗

includes(submission: :clients)

您收到此特定错误是因为您在查询中不正确地引用了city\u town。client.address.city\u town=>city\u town意味着此哈希中的键是嵌套在现有客户机变量中的值。这更为正确,可能会删除您刚才遇到的特定错误:

顺便说一句,评论员Pavan是正确的,:client在你的.includes语句中应该是单数。也就是说,我还将其扩展为包含前面的地址表,这可能会导致其他错误

# The .includes() parameters have been changed, and quotes have been added to the query.
scope :agency_referrals, -> (id, city_town) { includes(client: :address).where(agency_id: id, 'client.address.city_town' => city_town) }
另外,如果您正在寻找一种引用正确位置的非字符串方法,因为这是Ruby,而且我们喜欢我们的符号,那么您可以这样写:

includes(:clients).where(agency_id: id, client: { address: { city_town: city_town } } => city_town)
我个人觉得这不太干净,但它是一种更现代的格式


为了进一步参考,您可能希望查看。

您收到此特定错误,因为您在查询中不正确地引用了城市。client.address.city\u town=>city\u town意味着此哈希中的键是嵌套在现有客户机变量中的值。这更为正确,可能会删除您刚才遇到的特定错误:

顺便说一句,评论员Pavan是正确的,:client在你的.includes语句中应该是单数。也就是说,我还将其扩展为包含前面的地址表,这可能会导致其他错误

# The .includes() parameters have been changed, and quotes have been added to the query.
scope :agency_referrals, -> (id, city_town) { includes(client: :address).where(agency_id: id, 'client.address.city_town' => city_town) }
另外,如果您正在寻找一种引用正确位置的非字符串方法,因为这是Ruby,而且我们喜欢我们的符号,那么您可以这样写:

includes(:clients).where(agency_id: id, client: { address: { city_town: city_town } } => city_town)
我个人觉得这不太干净,但它是一种更现代的格式


为了进一步参考,您可能希望在推荐类中查看。

您在哪种型号中拥有该范围。我已经编辑了我的问题来说明这一点。Try includes:client而不是includes:clients我仍然会在client而不是clients中遇到类似的错误。在哪种模型中有该范围?在引用类中。我已经编辑了我的问题来说明这一点。尝试包括:客户端而不是包括:客户端我仍然会在客户端而不是客户端上遇到类似的错误。然后询问者必须将其引用更改为submission.client.address.city\u town,然后,询问者必须将其引用更改为submission.client.address.city\u town,并将一个完全不必要的表引入到混合中。抱歉,回复太晚。我会试试这个,并让你知道它是如何为迟来的答复。我会试试这个,让你知道它是怎么回事