Rails database relationships

Posted by Danny McClelland on Stack Overflow See other posts from Stack Overflow or by Danny McClelland
Published on 2010-04-26T15:20:37Z Indexed on 2010/04/26 15:23 UTC
Read the original article Hit count: 257

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

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about table-relationships