简体   繁体   English

has_many和belong_to关联出现问题,找不到方法

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

相关问题 数据库关联,类has_many和belonge_to可以属于同一对象吗? - Database Association, Can a class has_many and belong_to the same object? Rails has_many / belong_to协会 - Rails has_many/belong_to Associations 在rails中的has_many中的关联 - Associations in has_many , belong_to in rails 如何创建和保存具有has_many / belong_to关联的模型 - How to create and save an model that has a has_many/ belong_to association 如何在ruby中为用户和任务模型之间的has_many,belong_to关联创建迁移脚本 - How to create migration script for has_many, belong_to association between user and task model in ruby 创建一个新对象时,不能使用Emirates_to / has_many关系设置user_id字段 - Can't set user_id field when creating a new object, using a belong_to / has_many relationship 我怎样才能使用户具有has_many个对象,但每个对象都可以属于许多用户? - How can I make a user has_many objects but each object can belong_to MANY users? 关于belong_to,has_one,has_many的困惑 - Confusion regarding belong_to, has_one, has_many 用户has_many:课程,但课程不属于:用户 - user has_many :courses, but course doesn't belong_to :user 通过双归属表设置has_many - Setting has_many through double belong_to table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM