简体   繁体   English

从Rails中的不同模型调用数据

[英]Calling data from different model in Rails

I need to be able to call data from a different model - not just one field, but any of them. 我需要能够从不同的模型调用数据-不仅是一个字段,而且是其中的任何一个。

At the moment I have the following models: 目前,我有以下模型:

kase
person
company
party

I can call information from the company to the kase and from the person to the kase using this: 我可以使用以下方法将信息从公司致电给kase,从人致电给kase:

<li>Client Company Address: <span class="address"><%=h @kase.company.companyaddress %></span></li>
<li>Case Handler: <span><%=h @kase.person.personname %></span></li>

Those two work, however if I add the following: 这两个工作,但是如果我添加以下内容:

<li>Client Company Fax: <span><%=h @kase.company.companyfax %></span></li>
<li>Case Handler Tel: <span><%=h @kase.person.personmobile %></span></li>
<li>Case Handler Email: <span><%=h @kase.person.personemail %></span></li>

Any idea whats wrong? 知道怎么了吗?

Thanks, 谢谢,

Danny 丹尼

EDIT: I get the following error message: 编辑:我收到以下错误消息:

NoMethodError in Kases#show
Showing app/views/kases/show.html.erb where line #37 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.personname

The lines that are noted are as follows: 注意的行如下:

34: <div id="clientinfo_showhide" style="display:none">
35: <li>Client Company Address: <span class="address"><%=h @kase.company.companyaddress  %></span></li>
36: <li>Client Company Fax: <span><%=h @kase.company.companyfax %></span></li>
37: <li>Case Handler: <span><%=h @kase.person.personname %></span></li>
38: <li>Case Handler Tel: <span><%=h @kase.person.personmobile %></span></li>
39: <li>Case Handler Email: <span><%=h @kase.person.personemail %></span></li>
40: </div>

The model for the kase is as follows: kase的模型如下:

class Kase
belongs_to :company # foreign key: company_id
belongs_to :person # foreign key in join table

The model for the person is as follows: 该人的模型如下:

class Person
has_many :kases # foreign key in join table
belongs_to :company

The model for the company is as follows: 公司的模型如下:

class Company
has_many :kases
has_many :people
def to_s; companyname; end

Hope this helps! 希望这可以帮助!

只是要写一个正确的答案,问题是他在kasesperson_id kases当作string

是否在人员表中填充了数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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