# Newbie - Question on Parent child updating

**URL:** <https://www.ruby-forum.com/t/newbie-question-on-parent-child-updating/225962>\
**Category:** Rails\
**Created:** [August 5, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/newbie-question-on-parent-child-updating/225962 "2007-08-05T17:36:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![railerbaby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@railerbaby](https://www.ruby-forum.com/u/railerbaby)\
**Post date:** [August 5, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/newbie-question-on-parent-child-updating/225962/1 "2007-08-05T17:36:14Z")

</div>

All,

I’ve just started Rails and Ruby a week or so ago. Please forgive me  
for my ignorance. I started writing a small test application to test  
some of my new knowledge and have hit a wall. Would like some best  
practices.

I have 3 tables, as below…

CREATE TABLE `carts` (  
`id` int(11) NOT NULL auto\_increment,  
`name` varchar(255) NOT NULL,  
`quantity` int(11) NOT NULL,  
PRIMARY KEY (`id`)

CREATE TABLE `orders` (  
`id` int(11) NOT NULL auto\_increment,  
`name` varchar(255) NOT NULL,  
`address` varchar(255) NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO\_INCREMENT=2 ;

CREATE TABLE `order_details` (  
`id` int(11) NOT NULL auto\_increment,  
`name` varchar(255) NOT NULL,  
`quantity` int(11) NOT NULL,  
`order_id` int(11) NOT NULL,  
PRIMARY KEY (`id`)  
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO\_INCREMENT=4 ;

model cart.rb  
class Cart \< ActiveRecord::Base  
end

model order.rb  
class Order \< ActiveRecord::Base  
has\_many :order\_details  
end

model order\_detail.rb  
class OrderDetail \< ActiveRecord::Base  
belongs\_to :order  
end

In the carts table holds a shop cart with product details. For the  
ease of implementation, I’ve refrained from putting relationships with  
a product table or any session. Let’s just say, the field name “name”  
is the product name and “quantity” holds the quantity.

What I want to do is when the order is created via a form with the  
“name” and “address” fields, it will create

1. The order in the order table
2. Utilize the relationship in the model and update/create the  
order\_details from the contents of the cart table.

My question is if there is a simpler way to do the following with a  
more ruby like way. I’ve read in a book that you can do a  
@order.order\_detail \<\< @cart based on the code below.

Any advice is greatly appreciated.

What I’ve done in the controller is this …

order\_controller.rb  
class OrderController \< ApplicationController  
def new  
end  
def create  
@cart = Cart.find(:all)  
@order = Order.new(params[:order])  
@order.save!  
@cart.each do |cart|  
@order\_details = OrderDetail.new  
@order\_details.name = cart.name  
@order\_details.quantity = cart.quantity  
@order\_details.order\_id = @order.id  
@order\_details.save!  
end  
end  
end

* * *

view order/new.rhtml  
\<% form\_tag :action =\> ‘create’ do %\>

Name  
\<%= text\_field 'order', 'name' %\>

Address  
\<%= text\_field 'order', 'address' %\>

\<%= submit\_tag “Create” %\>  
\<% end %\>

view order/create.rhtml

# OK!
