REST Validation dilemma

I’ve recently upgraded to rails 2.0. Previously all my controllers were
traditional :controller/:action/:id based. I’ve now decided to go down
the REST route for new controllers etc.

So I’ve got a controller to do the create method for two models:
Licensee and licensee_address together in one form

The form tags looks like:

<% form_for([:agent, @licensee]) do |f| %>

<% fields_for(@licensee_address) do |fl| %>

and the controller code for the posted data looks like:

POST /agent/licensees

POST /agent/licensees.xml

def create
@licensee =[:licensee]) = true
@licensee.agent = @auth_agent
@licensee_address =[:licensee_address])
@licensee_address.licensee = @licensee

 respond_to do |format|
     Licensee.transaction do!!
         # must assign a unique licensee ref
         @licensee.licensee_ref = "%s%04d" %

[[0,3],] = true!
flash[:notice] = ‘Licensee was successfully created.’
format.html { redirect_to(agent_licensees_url) }
format.xml { render :xml => @licensee, :status => :created,
:location => @licensee }
rescue ActiveRecord::RecordInvalid => e
#make sure we catch any validation errors on the address
format.html { render :action => “new” }
format.xml { render :xml => @licensee.errors, :status =>
:unprocessable_entity }

Which all works splendidly if there are no validation errors. However,
if the are validation errors in the licensee_address, I have problems.

The licensee record was (temporarily) saved OK to the database so gets a
valid id and is no longer a “new” record. The saving of the
licensee_address fails. The transaction is rolled back so the database
is OK.

The problem comes with re-rendering the form to show the errors. The
licensee now looks like a valid record from the database and so the form
tag renders this as if it should post to an update method (i.e with a
PUT method and a valid id. This is clearly wrong. If I now correct the
error on the form and resubmit, the update method gets called and the
whole thing blows up!

Is there any way to tell an activerecord object that it was not really
saved to the database as it was rolled back? Is there any nice way to do
this kind of thing?


You could check the validity of both items before saving.

if @licensee.valid? and @licensee_address.valid?

Thanks for the suggestion.

Yes, I could, but my example was over simplified to save space. My point
is that I could have any number of operations/models in the transaction
and any one of them could cause an exception. I was just wondering if
there was a more general way of doing this is a safe fashion whilst
still adhering to “standard” principles.