Rails 数据库关系
我有三个模型想要相互交互。
加濑,个人和公司。
我(我认为)正确设置了关系:
class Kase < ActiveRecord::Base
#HAS ONE COMPANY
has_one :company
#HAS MANY PERSONS
has_many :persons
class Person < ActiveRecord::Base
belongs_to :company
class Company < ActiveRecord::Base
has_many :persons
def to_s; companyname; end
我已将选择字段放在创建新的 Kase 视图上,并创建新的人员视图,如下所示:
<li>Company<span><%= f.select :company_id, Company.all %> </span></li>
以上所有内容都成功显示了一个下拉菜单,其中动态填充了公司名称公司。
我想要做的是在 kase 和人员 show.html.erb 中显示公司记录的联系方式。
例如,如果我有一家名为“Acme, Inc.”的公司。并创建一个名为“Random Case”的新 Kase,并在创建新案例页面中选择“Acme, Inc.”。从公司下拉菜单中。然后我想在“随机案例”show.html.erb 上显示“Acme, Inc”以及“Acme, Inc. Mobile”等。
我希望这对某人有意义!
谢谢,
丹尼
编辑:kases_controller
def 显示 @kase = Kase.find(params[:id]) respond_to do |格式| format.html # show.html.erb format.xml { 渲染:xml => @加濑} format.pdf { 渲染:布局=>错误的 } 虾:虾=> { :背景=> “#{RAILS_ROOT}/public/images/jobsheet.png”, :左边距=> 0, :right_margin => 0, :top_margin => 0, :bottom_margin => 0, :页面大小=> 'A4'} 结束结束
I have three models that I want to interact with each other.
Kase, Person and and Company.
I have (I think) setup the relationships correctly:
class Kase < ActiveRecord::Base
#HAS ONE COMPANY
has_one :company
#HAS MANY PERSONS
has_many :persons
class Person < ActiveRecord::Base
belongs_to :company
class Company < ActiveRecord::Base
has_many :persons
def to_s; companyname; end
I have put the select field on the create new Kase view, and the create new Person view as follows:
<li>Company<span><%= f.select :company_id, Company.all %> </span></li>
All of the above successfully shows a drop down menu dynamically populated with the company names within Companies.
What I am trying to do is display the contact of the Company record within the kase and person show.html.erb.
For example, If I have a company called "Acme, Inc." and create a new Kase called "Random Case" and choose within the create new case page "Acme, Inc." from the companies drop down menu. I would then want to display "Acme, Inc" along with "Acme, Inc. Mobile" etc. on the "Random Case" show.html.erb.
I hope this makes sense to somebody!
Thanks,
Danny
EDIT: kases_controller
def show @kase = Kase.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @kase } format.pdf { render :layout => false } prawnto :prawn => { :background => "#{RAILS_ROOT}/public/images/jobsheet.png", :left_margin => 0, :right_margin => 0, :top_margin => 0, :bottom_margin => 0, :page_size => 'A4' } end end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您在问题中发布的内容,我认为您的模型关联是不完整的:
通过正确设置关联,您可以访问给定案例的公司属性:
- 或者对于给定的人:
您甚至可以访问公司通过个人案例:
I think your model associations are incomplete based on what you've posted in your question:
With the assocations set up correctly, you can then access a company's attributes for a given case:
—or for a given person:
You can even get to the company via a person's case:
如果我理解正确的话,您的显示文件将包含类似这样的内容来显示手机号码:
试一试,看看是否就是这样。
If I understand correctly, your show file would contain something like this to show the mobile number:
Give it a go, see if that's all it takes.