Rails 数据库关系

发布于 2024-08-30 06:53:02 字数 1295 浏览 11 评论 0原文

我有三个模型想要相互交互。

加濑,个人和公司。

我(我认为)正确设置了关系:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲸落 2024-09-06 06:53:02

根据您在问题中发布的内容,我认为您的模型关联是不完整的:

class Kase < ActiveRecord::Base
  has_one :company
  has_many :people # Rails should handle the correct plural here
end

class Company < ActiveRecord::Base
  has_many :people
  belongs_to :kase
end

class Person < ActiveRecord::Base
  belongs_to :company
  belongs_to :kase
end

通过正确设置关联,您可以访问给定案例的公司属性:

kase.company.name
kase.company.mobile

- 或者对于给定的人:

person.company.name
person.company.mobile

您甚至可以访问公司通过个人案例:

person.kase.company.name # etc...

I think your model associations are incomplete based on what you've posted in your question:

class Kase < ActiveRecord::Base
  has_one :company
  has_many :people # Rails should handle the correct plural here
end

class Company < ActiveRecord::Base
  has_many :people
  belongs_to :kase
end

class Person < ActiveRecord::Base
  belongs_to :company
  belongs_to :kase
end

With the assocations set up correctly, you can then access a company's attributes for a given case:

kase.company.name
kase.company.mobile

—or for a given person:

person.company.name
person.company.mobile

You can even get to the company via a person's case:

person.kase.company.name # etc...
睫毛上残留的泪 2024-09-06 06:53:02

如果我理解正确的话,您的显示文件将包含类似这样的内容来显示手机号码:

# in app/views/kases/show.html.erb
<h1><%=h kase.name %></h1>

<h2>Company Information</h2>
<ul>
  <li>Company Name: <%=h kase.company.name %></li>
  <li>Company Mobile: <%=h kase.company.mobile_phone %></li>
</ul>

试一试,看看是否就是这样。

If I understand correctly, your show file would contain something like this to show the mobile number:

# in app/views/kases/show.html.erb
<h1><%=h kase.name %></h1>

<h2>Company Information</h2>
<ul>
  <li>Company Name: <%=h kase.company.name %></li>
  <li>Company Mobile: <%=h kase.company.mobile_phone %></li>
</ul>

Give it a go, see if that's all it takes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文