views:

75

answers:

3

Hi Everyone,

I am making good progress with my first Rails app with a lot of help from the great community here at Stack Overflow.

I have a basic application that has the following models:

kase person company party

I have associated them in the 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 the moment, if I create a company and then go to create a new case (kase), I can choose from a drop down list the company I want (from the companies database) and then on the show view I can output the name of the chosen company for the case by using this code:

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

However, if I add a new Person using the same method - I can successfully assign a company to the person, but on the show view it only outputs the company ID number using this code:

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

If I change the above to:

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

I get the following error:

undefined method `company' for #<Person:0x105dc4938>

So it seems I can call the company ID, but nothing else from the company database, any ideas where I am going wrong?

Thanks,

Danny

+1  A: 

I am not sure why you can even call the company_id like that, are you sure you aren't doing:

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

I think the problem is that you are missing the reference to Company in your Person model. Try changing your Person model to:

class Person
has_and_belongs_to_many :kases # foreign key in join table
belongs_to :company

This should allow you to do lookups between people and companies in both directions.

latentflip
I tried that and as above I get an exception error if I do this:<%=h @kase.person.personname %>However, the following works:<%=h @kase.company.companyname %>
dannymcc
+1  A: 

not tested, did you try add:

belongs_to :company

in Person model?

ohho
+4  A: 

You have

class Person < ActiveRecord::Base
  has_and_belongs_to_many :kases
end

This means that you can do

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

But keep in mind that, in order for @person.company to work, you would need to have one of the following:

class Person < ActiveRecord::Base
  belongs_to :company  # option 1
  has_one :company     # option 2
end
Justice
+1 wow! Justice you type so long and still beats me by one second ;-)
ohho
The only thing that doesnt work is when I create a new person and choose the company from the drop down I get this error:undefined local variable or method `companyname' for #<Person:0x105e66cd8>EDIT: Nevermind - erroneous left in code from earlier today! Thanks All!
dannymcc
I have now fixed the error message, but if I use the following:<%=h @kase.person.personname %>It throws an exception error. Should that happen?
dannymcc
Since Kase-to-people is a has and belongs to many relationship, doing @kase.person isn't meaningful.It would be a bit like asking somebody, "hey what is your friend's name?": without knowing which friend you are referring to it would be impossible to answer the question.Something like this should work: <%=h @kase.people.first.personname %>.This is finding the first person in the list of people associated with the kase, and getting their name. Note that person has changed to people due to it being a has and belongs to many relationship.Hope that makes sense.
latentflip
Thats great, thanks. I think I understand.
dannymcc