![](/img/trans.png)
[英]Rails has_many :through and has_one :through associations
[英]Rails :has_many of a :has_one through
我在建立Rails中的模型关系时遇到问题。
我有一个用户。 一个用户可以有很多请求。 一个请求可以有一个响应。 我这样建立模型:
Class User < ActiveRecord::Base
has_many :user_requests
has_many :request_responses, through: :user_requests
end
Class UserRequest < ActiveRecord::Base
belongs_to :user
has_one :request_response
end
Class RequestResponse < ActiveRecord::Base
belongs_to :user_request
end
每当我尝试做类似的事情:
UserRequest.request_response.id
我收到错误消息,指出该关系不存在或表中的列不存在。 我的人际关系设置不正确吗?
您将得到错误:
UserRequest.request_response.id
因为:
request_response
是UserRequest
的类方法。 request_responses
,而不是request_response
,因此调用user. request_response
user. request_response
也不起作用。 呼叫user.request_response_ids
,其中user = User.first
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.