# Validations and Transactions

**URL:** <https://www.ruby-forum.com/t/validations-and-transactions/107775>\
**Category:** Rails\
**Created:** [June 26, 2007, 3:24am UTC](https://www.ruby-forum.com/t/validations-and-transactions/107775 "2007-06-26T03:24:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cammo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cammo](https://www.ruby-forum.com/u/cammo)\
**Post date:** [June 26, 2007, 3:24am UTC](https://www.ruby-forum.com/t/validations-and-transactions/107775/1 "2007-06-26T03:24:08Z")

</div>

Hey I have a rather complex record set with lots of associated models  
all being save by 1 form. So I’ve wrapped the whole thing in a  
transaction block, but, does anyone know how I can get Active Record  
to return my validation errors?

---

<div class="post-metadata">

**Author:** ![cammo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cammo](https://www.ruby-forum.com/u/cammo)\
**Post date:** [June 26, 2007, 4:14am UTC](https://www.ruby-forum.com/t/validations-and-transactions/107775/2 "2007-06-26T04:14:10Z")

</div>

On 6/26/07, cammo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey I have a rather complex record set with lots of associated models  
> all being save by 1 form. So I’ve wrapped the whole thing in a  
> transaction block, but, does anyone know how I can get Active Record  
> to return my validation errors?

Hey Cammo,

This may not be straight forward and it depends on how your transaction  
is  
setup. If you are creating the objects during the transaction, it will  
only  
process the entities until it fails, potentially leaving some objects  
uninstantiated.

AR errors are attached to the AR instance when it is checked for  
validation. So save, create, valid? calls will all run through the  
validation steps of a model and attach any errors that were found to  
that  
instance.

ie.

@my\_model.valid? #=\> will attach the errors to the @my\_model instance.

You get these with a number of methods

@my\_model.errors  
or in a view  
error\_messages\_for ‘my\_model’

If you want to get all the objects to return their errors you might use  
something like this in your controller. (sorry can’t check syntax here  
)

def create

@my\_objects = list\_of\_object\_params.map{ |obj\_p| MyModel.new( obj\_p ) }

MyObject.transaction do  
@my\_objects.each{ |obj| obj.save! }  
end

rescue  
#There is an issue in the transaction here.  
@my\_objects.each{ |obj| obj.valid? } #=\> ensure validation is run on  
all  
objects  
render / redirect / whatever  
end

then in your view

\<% @my\_objects.each do |obj| %\>  
\<% @tmp = obj %\>   
\<%= error\_messages\_for ‘tmp’ %\>  
\<% end %\>

This may not work, it’s not very elegant, and I’ve never tried it before  
😉  
But it may give you some clues to move forward.

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![cammo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19b73/32.png) [@cammo](https://www.ruby-forum.com/u/cammo)\
**Post date:** [June 26, 2007, 4:44am UTC](https://www.ruby-forum.com/t/validations-and-transactions/107775/3 "2007-06-26T04:44:11Z")

</div>

Yeah cool. It’s kinda a shortcoming of transactions, I can see why it  
works that way, but it seems a little stupid.

Thanks again, I’ll investigate.
