Rails database relationships
- by Danny McClelland
Hi Everyone,
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