views:

55

answers:

2

Hi Everyone,

I am slowly getting the hang of Rails and thanks to a few people I now have a basic grasp of the database relations and associations etc. You can see my previous questions here: http://stackoverflow.com/questions/2714621/rails-database-relationships

I have setup my applications models with all of the necessary has_one and has_many :through etc. but when I go to add a kase and choose from a company from the drop down list - it doesnt seem to be assigning the company ID to the kase.

You can see a video of the the application and error here: http://screenr.com/BHC

You can see a full breakdown of the application and relevant source code at the Git repo here: http://github.com/dannyweb/surveycontrol

If anyone could shed some light on my mistake I would be appreciate it very much!

Thanks,

Danny

+1  A: 

You have setup your Kase and Company models as a one-to-one relationship (see http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html). This is probably not what you intended. Maybe if you could explain your intended relationship I could tell you where your mistake is?

class Company < ActiveRecord::Base
  has_many :kases
  has_many :people
end

class Kase < ActiveRecord::Base
  belongs_to :company    # foreign key: company_id
  has_and_belongs_to_many :people    # foreign key in join table
end

class Person < ActiveRecord::Base
  has_and_belongs_to_many :kases    # foreign key in join table
end

Relevant parts shown only. This should be a step in the right direction. You will need a join table for the many-to-many relationship, or alternatively, to model it using "has_many :through". Depends on whether you need to store other properties on the join. See link above for details.

Jakob Kruse
Hi Jakob, Basically it needs to be as follows:A case would have only one company, but a company will have many cases. A company would have many people, and each person can be linked to a case, through the company.Does that makes sense?Thanks,Danny
dannymcc
Most of it makes sense :) Companies have many cases and people, that's clear. I'm not sure what you mean by linking a person to a case "through the company"?
Jakob Kruse
Basically if we add a case, we would choose a company and then we would want a list of people within that company to choose from.Thanks,Danny
dannymcc
OK, that's a many-to-many relationship as I understand what you're saying. One case could have many people, and one person could work on many cases. I'll add to my example.
Jakob Kruse
ok, I have made the changes to the models, but I still seem to be getting the same error. I have pushed the changes to the Github repo linked to above.
dannymcc
Hi Jakob, I have had a read of the link above but I am still struggling to get my head around it. I have a party model for the link tables which has the following: http://pastie.org/937787Am I on the right path? If so, what next?
dannymcc
Sorry, the join table needs to be named "kases_people" for rails to pick it up automatically
Jakob Kruse
If I am following the other answer of not having the has_and_belongs_to_many do I still need the kases_people?
dannymcc
No, but do note that in the other example, any given person can only be assigned to one case. If that is how you wanted it, then my assumption about many-to-many was incorrect.
Jakob Kruse
ahh, I missed that completely!I guess I need to start again then and use your example. When you said to add the join table called kases_people, I have a model called partied as noted above. Should that table have a column called kases_people?
dannymcc
this `parties` table should be named `kases_people`.
j.
OK, I have renamed the table kases_people. I can get the cases working, if I add a case it will show the company I chose when I view it (show.html.erb) - but the person show won't display the company I chose when creating the record. Any ideas?
dannymcc
That's a lot of questions for one thread. I think you should try to isolate one problem at a time, and put each in a separate question here on SO, preferably getting your model setup correctly first (you can use IRB to validate that your model works as expected)
Jakob Kruse
ok, I'm as new to Stack Overflow as I am to Rails. Appreciate your advice!
dannymcc
+1  A: 

I believe It should be

class Company < ActiveRecord::Base
   has_many :people
   has_many :kases
end

class Kase < ActiveRecord::Base
   belongs_to :company
   belongs_to :person
end

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

In your view (app/views/kases/new.html.erb) you have

<li>Company Select<span><%= f.select :company_id, Company.all %></span></li>

Try changing the select part to

<%= f.select :company_id, Company.all.collect {|m| [m.name, m.id]} %>

Suggestion

I also notice that you have four methods in your controller to find Kases by status. You can do this in your model, using named_scope. It's like this:

named_scope :active, :conditions => {:kase_status => 'Archived'}

And then, wherever you need to show only active Kases, you call Kase.active. The same for the other status.

j.
ok, I have made the changes to the models, but I still seem to be getting the same error. I have pushed the changes to the Github repo linked to above.
dannymcc
That changes the drop down list's content to "<option value="3">DannyWeb Limited </option>, but still returns the same error message. I did change the name part to companyname to match the column in the company database. Is that right?
dannymcc
i edited my answer... i was making a mistake.
j.
Hmm, same problem - would you be interested in helping me work this out and charging me for your time?I just cannot work it out!
dannymcc
You have this line in your controller, right `@company = Company.find(params[:company_id])` ? You have no `params[:company_id]`, but `params[:kase][:company_id]`.
j.
ahh, ok thats removed the error message.What would the code be to show the company name assigned to the kase?
dannymcc
Nevermind!<%=h @kase.company.companyname %>
dannymcc
How do I now list all people associated under a company when I create a new case?
dannymcc
That's another question... I believe you should ask it separately :] Anyway, I'll think and try to answer that.
j.
ok, but within this question (I think that's ok) should I be able to use the same code <%=h @kperson.company.companyname %> to display the listed under company?
dannymcc
If i understood it correctly, yes. This code should work.
j.