# Creating a master/detail record in one shot

**URL:** <https://www.ruby-forum.com/t/creating-a-master-detail-record-in-one-shot/68071>\
**Category:** Rails\
**Created:** [September 8, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/creating-a-master-detail-record-in-one-shot/68071 "2006-09-08T22:33:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stedak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@Stedak](https://www.ruby-forum.com/u/Stedak)\
**Post date:** [September 8, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/creating-a-master-detail-record-in-one-shot/68071/1 "2006-09-08T22:33:03Z")

</div>

How would I set up the controllers so that I can insert a detail record  
with information from the web page and the master record at the same  
time?

For instance if I had a new book entry in the book database I want to  
add a new author record in the author database - assuming the author  
did not exist.

Thanks!

---

<div class="post-metadata">

**Author:** ![Stedak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@Stedak](https://www.ruby-forum.com/u/Stedak)\
**Post date:** [September 9, 2006, 2:01am UTC](https://www.ruby-forum.com/t/creating-a-master-detail-record-in-one-shot/68071/2 "2006-09-09T02:01:24Z")

</div>

On 9/8/06, Stedak [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How would I set up the controllers so that I can insert a detail record  
> with information from the web page and the master record at the same  
> time?
> 
> For instance if I had a new book entry in the book database I want to  
> add a new author record in the author database - assuming the author  
> did not exist.

It’s easier than you think.

In general…

# set up the master record …

# master = Master.find(id), etc.

# set up the detail records…

# associate the 2

master.details \<\< detail

# or, master.details = details

# or, master.details \<\< details

# profit!

if master.save  
success  
else  
error  
end
