# REST Validation dilemma

**URL:** <https://www.ruby-forum.com/t/rest-validation-dilemma/225990>\
**Category:** Rails\
**Created:** [January 20, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/rest-validation-dilemma/225990 "2008-01-20T15:15:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdgary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@jdgary](https://www.ruby-forum.com/u/jdgary)\
**Post date:** [January 20, 2008, 3:15pm UTC](https://www.ruby-forum.com/t/rest-validation-dilemma/225990/1 "2008-01-20T15:15:25Z")

</div>

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.new(params[:licensee])  
@licensee.active = true  
@licensee.agent = @auth\_agent  
@licensee\_address = LicenseeAddress.new(params[:licensee\_address])  
@licensee\_address.licensee = @licensee

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

```

[@licensee.name[0,3],@licensee.id]  
@licensee.active = true  
@licensee.save!  
end  
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  
@licensee\_address.valid?  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @licensee.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

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?

Cheers,  
Gary.

---

<div class="post-metadata">

**Author:** ![jdgary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@jdgary](https://www.ruby-forum.com/u/jdgary)\
**Post date:** [January 20, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/rest-validation-dilemma/225990/2 "2008-01-20T15:43:30Z")

</div>

You could check the validity of both items before saving.

if @licensee.valid? and @licensee\_address.valid?  
…

---

<div class="post-metadata">

**Author:** ![jdgary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@jdgary](https://www.ruby-forum.com/u/jdgary)\
**Post date:** [January 20, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/rest-validation-dilemma/225990/3 "2008-01-20T16:46:22Z")

</div>

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.

Cheers,  
Gary.
