Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 where子句中的嵌套关联不';无法生成有效的SQL_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails where子句中的嵌套关联不';无法生成有效的SQL

Ruby on rails where子句中的嵌套关联不';无法生成有效的SQL,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我有以下型号 class User < ActiveRecord::Base has_many :documents has_many :requests end class Document < ActiveRecord::Base has_many :requests belongs_to :user end class Request < ActiveRecord::Base belongs_to :document belongs_to

我有以下型号

class User < ActiveRecord::Base
  has_many :documents
  has_many :requests
end

class Document < ActiveRecord::Base
  has_many   :requests
  belongs_to :user
end

class Request < ActiveRecord::Base
  belongs_to :document
  belongs_to :requester, class_name: 'User', foreign_key: 'user_id'
end
我犯了一个错误

ERROR: column documents.user does not exist

显然可以通过将
user:self
替换为
user\u id:id
来解决这个问题,但我不明白为什么活动记录不能与
user:self
一起使用。此行为是否有文档记录?

请求属于文档,因此此部分查询在此上下文中无效:
文档:{user:self}

从:

对于“属于”关系,如果使用ActiveRecord对象作为值,则可以使用关联键指定模型

这意味着要使
self
工作,关系必须是
beliens\u to
,以便您可以获取属于特定文档的请求

之所以
user:id
有效,是因为
documents
表碰巧有一个
user\u id

ERROR: column documents.user does not exist