views:

133

answers:

2

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

EDIT: kases_controller

def show
@kase = Kase.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.xml  { render :xml => @kase }
  format.pdf { render :layout => false }

  prawnto :prawn => { 
             :background => "#{RAILS_ROOT}/public/images/jobsheet.png",

             :left_margin => 0, 
             :right_margin => 0, 
             :top_margin => 0, 
             :bottom_margin => 0, 
             :page_size => 'A4' }
end   end
A: 

If I understand correctly, your show file would contain something like this to show the mobile number:

# in app/views/kases/show.html.erb
<h1><%=h kase.name %></h1>

<h2>Company Information</h2>
<ul>
  <li>Company Name: <%=h kase.company.name %></li>
  <li>Company Mobile: <%=h kase.company.mobile_phone %></li>
</ul>

Give it a go, see if that's all it takes.

Josh Pinter
Sadly, this results in the same error message as above. Thanks though!
dannymcc
Using all the code John above said: If you're saying kase :has_one company than the connection is based on a kase_id column in the company table. Also, you're using company names for id values. That's not right. They are the record number of the company, not the names.Try that.
Josh Pinter
Originally it was just the company_id as numbers etc. but I changed that as the drop down on the new case page didn't show the company names just numeric values.
dannymcc
+3  A: 
John Topley
Great, I have made those changes and will try calling the attributes now.
dannymcc
Obviously I guessed what your model attributes are called, so you need to use the correct names as defined in your database migrations.
John Topley
If I add the following: <li>Company: <span><%=h @kase.company.companyname %></span></li>I get the following error: NoMethodError in Kases#showShowing app/views/kases/show.html.erb where line #14 raised:You have a nil object when you didn't expect it!The error occurred while evaluating nil.companynameAny idea what I am doing wrong?Thanks,Danny
dannymcc
I'm assuming that you actually have some cases with an associated company in your database, right? Can you edit your question to show us the `show` action within your `KasesController`.
John Topley
Sure, edited above. Really appreciate your help on this!
dannymcc
OK, so for some reason Rails is saying that your @kase instance doesn't have a company associated with it. Can you try running the Rails console (`script/console`) and finding a kase that you know has an associated company and then seeing what `kase.company` returns?
John Topley
Here is a screenshot of the database: http://img.skitch.com/20100426-pyi8uqks8ajs4mbjm43xe24gqn.jpgThe database is purely in development so there are only two test cases/kases. Both are showing a company in the company id field.
dannymcc
Where does that `company_id` field in your screenshot come from? Is it an actual column within your `kases` table, or is it as a result of executing a query?
John Topley
That's actually a column within the kases table.Sorry if I'm doing something wrong - I'm really new to RoR
dannymcc
If it helps, the whole application is on my Github page: http://github.com/dannyweb/surveycontrol
dannymcc
I'll try to take a look at it tomorrow.
John Topley
Looking at your controller code on GitHub, it looks like you're just creating the individual model instances and not creating associations between them, which means that the foreign keys don't get set in the database. Have a look at sections 4.1, 4.3.1.12 and 4.3.1.13 in http://guides.rubyonrails.org/association_basics.html
John Topley
Thanks John. I have read through those sections, to be honest I think they have confused me a little.From my understanding; I have added the has_many and belongs_to etc. to the relevant controllers. This is based on one of the examples on the Rails Wiki:class Supplier < ActiveRecord::Base has_one :account, :include => :representativeendclass Account < ActiveRecord::Base belongs_to :supplier belongs_to :representativeendclass Representative < ActiveRecord::Base has_many :accountsendDo I also need to add the class_names etc?Thanks,Danny
dannymcc
@Danny, the structural relationships between your models are fine. The problem is that when your models are created and saved in your controllers when your app runs, you're not also setting the associations. Effectively your model instances are lonely islands with no relationships to other models. So the foreign keys never get set in the database. Look at this http://guides.rubyonrails.org/getting_started.html#generating-a-controller in particular the line `@comment = @post.comments.build(params[:comment]` in the `new` action of the `CommentsController`.
John Topley
ahh, ok that helps a little.So for my new kase def it would be something like this:?@kase = @company.kaseid.buildFor the project I am working on, its purely for me to try and learn Rails, would you be interested in getting this working for me with comments in the code and charging me for your time?Thanks,Danny
dannymcc
That's on the right track, yes. Or you can use `create` instead of `build` to have the association created and saved to the database in one step. I appreciate your offer, but unfortunately I don't have a lot of free time at the moment. I think you'd probably get a better return for your money anyway by buying some or all of these screencasts: http://pragprog.com/screencasts/v-rbar/everyday-active-recordPeepCode have many great Rails screencasts too: http://peepcode.com/And continue to ask questions here! I wish Stack Overflow had been around when I was first learning Rails.
John Topley
Ok, thank you anyway. I do really appreciate your time though!So if I had: @kase = @company.kaseid.create in my kase controller, is that saying when a new Kase is created, add the kaseid to the company model?I think this is the bit I dont understand. What that line is actually going to do.Thanks,Danny
dannymcc
It should be `@kase = @company.kases.create(params[:kase])` All it does is create a new case through the association i.e. add the new case to the company's existing collection of cases and save it to the database all in one hit. In database terms, it sets the `kase_id` column for that company record to the ID of the case record that owns it. The `params[:kase]` bit is a bit of Rails' magic that sets the attributes of the new case to the values from the submitted form. You can test it from the Rails console if you want: `@kase = @company.kases.create(:jobno => 'foo', :clientref => 'bar' etc.)`
John Topley
Does that mean that the way I have it currently, each company can only have one case/Kase?
dannymcc
You have declared a one-to-one relationship between case and company.
John Topley
Hmm, I'm unsure what I need to declare as each case only has one company but each company will have multiple cases.So should the case belong_to company and company has_many cases?
dannymcc
It sounds like it, yes.
John Topley
Ok, I have made all of the relevant changes to the models (I think!) and added the params line as above to kase_controller but I get the following error:NoMethodError in KasesController#newYou have a nil object when you didn't expect it!The error occurred while evaluating nil.kases
dannymcc
I'm sorry Danny, I can't spend any more time helping you on this at the moment. My advice is to get yourself a good Rails book or the ActiveRecord screencasts I mentioned earlier. Good luck!
John Topley
No problem, I am buying those screencasts as we speak!Thanks for your help, very much appreciated!
dannymcc