繁体   English   中英

Rails的:has_many通过

[英]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

因为:

  1. request_responseUserRequest的类方法。
  2. 关联定义为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM