database - Rails show company name rather than company ID -


i making progress first rails app lot of great community here @ stack overflow.

i have basic application has following models:

kase person company party

i have associated them in following way:

class kase    belongs_to :company # foreign key: company_id has_and_belongs_to_many :people # foreign key in join table  class person has_and_belongs_to_many :kases # foreign key in join table  class company has_many :kases has_many :people  class party has_and_belongs_to_many :people has_and_belongs_to_many :companies 

at moment, if create company , go create new case (kase), can choose drop down list company want (from companies database) , on show view can output name of chosen company case using code:

<li>client company: <span><%=h @kase.company.companyname %></span></li> 

however, if add new person using same method - can assign company person, on show view outputs company id number using code:

<li>person company: <span><%=h @person.company.company_id %></span></li> 

if change above to:

<li>person company: <span><%=h @person.company.companyname %></span></li> 

i following error:

undefined method `company' #<person:0x105dc4938> 

so seems can call company id, nothing else company database, ideas going wrong?

thanks,

danny

you have

class person < activerecord::base   has_and_belongs_to_many :kases end 

this means can do

@person = person.find(1) @person.kases.each |kase|   puts kase.company.name end 

but keep in mind that, in order @person.company work, need have 1 of following:

class person < activerecord::base   belongs_to :company  # option 1   has_one :company     # option 2 end 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -