# Multiple models from one controller

**URL:** <https://www.ruby-forum.com/t/multiple-models-from-one-controller/92685>\
**Category:** Rails\
**Created:** [April 23, 2007, 1:26am UTC](https://www.ruby-forum.com/t/multiple-models-from-one-controller/92685 "2007-04-23T01:26:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ikiini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ikiini](https://www.ruby-forum.com/u/ikiini)\
**Post date:** [April 23, 2007, 1:26am UTC](https://www.ruby-forum.com/t/multiple-models-from-one-controller/92685/1 "2007-04-23T01:26:05Z")

</div>

Hey all,

I am new to Ruby on Rails and need some beyond basics help. I have a  
the agile web dev with rails 2nd ed. And i have looked at a few  
tutorials online. However none has covered real life webapps with  
multiple tables in a DB where some depend on others. let me cut to the  
chase. I have a app with 3 tables. 2 of the tables are directly  
related meaning that i can’t insert into one with out inserting the  
corresponding values into the other. now i have figure out with some  
help from IRC how to do this. but now i have a problem. I need to have  
validattion on both models before the inserts are made. what is  
happening is that if validation passes on the 1st, that inserts then  
the 2nd fails and that doesn’t insert. so right there i have a  
problem. My question is how do i handle such a situation. Thanks for  
the help

-B

---

<div class="post-metadata">

**Author:** ![ikiini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ikiini](https://www.ruby-forum.com/u/ikiini)\
**Post date:** [April 23, 2007, 1:42am UTC](https://www.ruby-forum.com/t/multiple-models-from-one-controller/92685/2 "2007-04-23T01:42:06Z")

</div>

Read up on validates\_associated[1]. Also consider:

begin  
order = order.new(params[:order])  
line\_item = table2.new(params[:line\_item])  
order.line\_items \<\< line\_item  
order.save!  
rescue

# something went wrong

end

This is ‘air code’ and clearly a simplistic example, but what should  
happen here is that you create a new order. orders has an associated  
table, line\_items (probably a has\_many relationship). When you use  
save!, an exception is created if there is a problem such as model  
validation failure. Because you used validates\_associated, this  
bubbles failures in the associated item up. When the exception  
happens, the transaction block causes a rollback, leaving the  
database in its original state. The Rails dox [2] say that “save and  
destroy are automatically wrapped in a transaction.”

Best to test this in script/console and observe its actual behavior.

Hope this helps.

[1] [http://railsmanual.org/module/](http://railsmanual.org/module/)  
ActiveRecord::Validations::ClassMethods/validates\_associated  
[2] [http://railsmanual.org/module/ActiveRecord%3A%3ATransactions%3A%](http://railsmanual.org/module/ActiveRecord%3A%3ATransactions%3A%25)  
3AClassMethods

---

<div class="post-metadata">

**Author:** ![ikiini](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ikiini](https://www.ruby-forum.com/u/ikiini)\
**Post date:** [April 23, 2007, 2:28am UTC](https://www.ruby-forum.com/t/multiple-models-from-one-controller/92685/3 "2007-04-23T02:28:09Z")

</div>

i just found exactly what i need on the rails forum and it works  
great. Check out this link.[http://railsforum.com/viewtopic.php?id=717](http://railsforum.com/viewtopic.php?id=717).  
thanks however for your very quick response.

-B
