1

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: 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

Community
  • 1
  • 1
dannymcc
  • 3,744
  • 12
  • 52
  • 85

2 Answers2

1

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
  • 2,458
  • 18
  • 17
  • 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 Apr 27 '10 at 18:07
  • 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 Apr 27 '10 at 18:31
  • 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 Apr 27 '10 at 18:33
  • 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 Apr 27 '10 at 18:36
  • 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 Apr 27 '10 at 18:53
  • 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/937787 Am I on the right path? If so, what next? – dannymcc Apr 27 '10 at 18:59
  • Sorry, the join table needs to be named "kases_people" for rails to pick it up automatically – Jakob Kruse Apr 27 '10 at 19:30
  • If I am following the other answer of not having the has_and_belongs_to_many do I still need the kases_people? – dannymcc Apr 27 '10 at 19:37
  • 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 Apr 27 '10 at 19:41
  • 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 Apr 27 '10 at 19:56
  • this `parties` table should be named `kases_people`. – Ju Nogueira Apr 27 '10 at 20:11
  • 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 Apr 27 '10 at 20:17
  • 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 Apr 27 '10 at 20:20
  • ok, I'm as new to Stack Overflow as I am to Rails. Appreciate your advice! – dannymcc Apr 27 '10 at 20:24
1

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.

Ju Nogueira
  • 8,435
  • 2
  • 29
  • 33
  • 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 Apr 27 '10 at 18:43
  • That changes the drop down list's content to ", 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 Apr 27 '10 at 19:04
  • 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 Apr 27 '10 at 19:14
  • 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]`. – Ju Nogueira Apr 27 '10 at 19:22
  • ahh, ok thats removed the error message. What would the code be to show the company name assigned to the kase? – dannymcc Apr 27 '10 at 19:26
  • How do I now list all people associated under a company when I create a new case? – dannymcc Apr 27 '10 at 19:28
  • That's another question... I believe you should ask it separately :] Anyway, I'll think and try to answer that. – Ju Nogueira Apr 27 '10 at 19:31
  • 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 Apr 27 '10 at 19:33