[英]Issue with has_many and belong_to association, can't find method
This is a many(proposaltracking)-to-one(partner) relationship. 这是一个多(建议跟踪)到一个(伙伴)的关系。
I am getting undefined method 'reference' for ProposalTracking:Class
error with @company.proposalTracking.reference
我正在使用
@company.proposalTracking.reference
undefined method 'reference' for ProposalTracking:Class
错误的undefined method 'reference' for ProposalTracking:Class
When I run @company.proposalTracking
it returns me the ProposalTracking object. 当我运行
@company.proposalTracking
它将返回ProposalTracking对象。
Here is my model: 这是我的模型:
class ProposalTracking < ActiveRecord::Base
set_table_name "Proposal_Tracking"
belongs_to :partner
end
class Partner < ActiveRecord::Base
has_many :proposalTracking
end
What I want to get is the attributes of proposalTracking
like 我想要得到的是
proposalTracking
的属性,例如
@company = Partner.find(params[:id])
@company.proposalTracking.reference
but this results in the error undefined method 'reference' for ProposalTracking:Class
但这会导致
undefined method 'reference' for ProposalTracking:Class
出现错误的undefined method 'reference' for ProposalTracking:Class
I have read solutions where it is because since it is a one-to-many relation, the partner may have more than one track proposal so I would have to grab the first one using .first
but I tried this and it then says 我已经读过解决方案,因为这是一对多的关系,因此合作伙伴可能有多个建议,因此我必须使用
.first
来获取第一个建议,但是我尝试过然后它说
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.reference
Any help would be much appreciated! 任何帮助将非常感激!
It should be 它应该是
class Partner < ActiveRecord::Base
has_many :proposal_trackings
end
and you can access it via @company.proposal_trackings
. 您可以通过
@company.proposal_trackings
访问它。 But that's actually an array of ProposalTracking instances (because of has_many) that you have to iterate over to get each attribute 但这实际上是一个ProposalTracking实例数组(由于has_many),您必须对其进行迭代以获取每个属性
@company.proposal_trackings.map(&:reference)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.