I hav page where I am saving a "client" record and a "person" record.
Sometimes the "person" is an existing record and sometimes it is a new
record. The "client" is always new.
Here is some code:
class Person < ActiveRecord::Base
class Client < ActiveRecord::Base
@client = Client.new(params[:client])
begin #check if the person already exists
rescue #otherwise create new person
flash[:notice] = 'Client was successfully created.'
redirect_to :action => 'edit', :id => @client.id
render :action => 'new'
Im not really sure what the best way and order to do the saves is.
If I try to save client first then it wont have a person_id yet and
will fail validation.
If I save the person first and then the client fails validation for
some other reason the code sort of works in that it rolls back but the
@person record has an id set even though the save failed. It also
appears to return false for @person.new_record?
Is there a tidier way to do this? I have tried @client.person.save and
various other combinations but it is not really clear what actually
Any comments appreciated.