Eric Sloane
2005-Dec-06  22:56 UTC
Real Dumb Newbie Q: Saving multiple data sets from one form
Hi,
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.
Sorry it''s such a dumb question. Help much appreciated
Kind Regards,
Eric.
joost baaij
2005-Dec-08  16:39 UTC
Re: Real Dumb Newbie Q: Saving multiple data sets from one f
def create @job = Job.new(params[:job]) @address = Address.new(params[:address]) @job.address << @address # Then save the objects end This could probably be rewritten using the .create method (which instantiates an object and saves it right away). -- Posted via http://www.ruby-forum.com/.