[英]Call more than one item from a different model?
At the moment in my application I can select a company from the company model when creating a new kase in the kase model. 目前,在我的应用程序中,我可以在kase模型中创建新的kase时从公司模型中选择一家公司。
<ul id="kases_new">
<li>Company<span><%= f.select :company_id, Company.all.collect {|m| [m.companyname, m.id]} %></span></li>
This shows a list of the companies and then when I choose one it adds the company ID to the company_id field in the kases table. 这显示了公司列表,然后当我选择一个公司列表时,将公司ID添加到kases表中的company_id字段中。
I want to be able to add a second company to a kase but I don't know what the next step is. 我希望能够向kase添加第二家公司,但我不知道下一步是什么。 I have created a field in the kases table called appointedsurveyor_id so when I select the company it adds the company id to the appointedsurveyor_id field. 我已经在kases表中创建了一个名为namedsurveyor_id的字段,因此,当我选择公司时,它将公司ID添加到namedsurveyor_id字段中。
This works, but I need to be able to call information regarding that company like I would the first company. 这行得通,但是我需要像第一个公司一样能够调用有关该公司的信息。
<%= @kase.appointedsurveyor_id %>
The above returns the company_id number, but how do I return the company name, address and so on? 上面返回的是company_id号,但是如何返回公司名称,地址等?
For the first company it is as simple as: 对于第一家公司,它很简单:
<li>Fax: <span><%=h @kase.company.companyfax %></span></li>
but if I do that a second time, I just get the first companies details. 但是,如果我第二次这样做,我只会获得第一家公司的详细信息。
Thanks, 谢谢,
Danny 丹尼
In your Kase model: 在您的Kase模型中:
belongs_to :surveyor,
:class_name => "Company",
:foreign_key => "appointedsurveyor_id"
Then you have access to it via @kase.surveyor
and have access to all the Company
info. 然后,您可以通过@kase.surveyor
进行访问,并可以访问所有Company
信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.