Peter D. wrote:
I was only trying to help you get an answer to your question. Specifically I
Cheers,
Peter D.
Blog: http://www.RealityForge.org
OK Peter - we’ve got off on the wrong foot it seems - I have no
intention of causing offence. I thought I had included enough
information since it’s a trivial newbie question regarding a lack of
understanding of HABTM, ActiveRecord, and, specifically how to code a
save in a controller on the the three tables, i.e. jobs, addresses and
jobs_addresses (which is specified as a join_table in the model)
I’m using ruby 1.8.2, rails 0.14.3 if that is of any use. The issue is
I have a couple of tables, Jobs and Addresses with a HABTM
relationship >through Jobs_Addresses and I have a model and controller
for both jobs >and addresses. What would be the best way to code a
create method to >save the two data sets? I tried;
def create
@job = Job.new(params[:job])
@address = Address.new(params[:address])
if @job.save && @address.save
flash[:notice] = ‘Job was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
Which resulted in;
NoMethodError in Job#create
undefined method `address’ for #Job:0x3912190
Request
Parameters: {“commit”=>“Save Job Data”, “job”=>{“job_number”=>“1984”,
“closed”=>“false”, “job_phase_id”=>“1”, “job_type_id”=>“1”},
“address”=>{“country”=>“Australia”, “suburb”=>“Spanish Harlem”,
“address_1”=>“Level 42”, “address_2”=>“77, Sunset Strip”,
“state”=>“Confused”}}
So all the data is there I just don’t know how to split the save to
two >tables.
Where my problem - or lack of understanding - lies, is specifically
within the controller.
The models state;
class Job < ActiveRecord::Base
has_and_belongs_to_many :people
has_many :milestones
has_many :job_phases
has_many :people
has_and_belongs_to_many :addresses, :join_table => “jobs_addresses”
has_many :reminders
has_many :discussions
has_many :actions
has_one :jobtype
has_many :documents
has_and_belongs_to_many :organisations
validates_associated :address
end
class Address < ActiveRecord::Base
has_one :address_type
has_and_belongs_to_many :jobs, :join_table => “jobs_addresses”
has_one :organisation
has_many :people
end
As to the stack trace - well I’ve stated the results and the request
paramenters - what more do you need? As to relationships outside of
Jobs/Addresses, I’m not concerned with those right now, getting over
this fundamentally dumb(on my part) lack of understanding will keep me
going for a while ;~)
Hope this is enough info for you - and thanks, truly
Kind Regards,
Eric