# Creating two activerecords with one form

**URL:** <https://www.ruby-forum.com/t/creating-two-activerecords-with-one-form/118850>\
**Category:** Rails\
**Created:** [October 31, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/creating-two-activerecords-with-one-form/118850 "2007-10-31T17:41:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [October 31, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/creating-two-activerecords-with-one-form/118850/1 "2007-10-31T17:41:05Z")

</div>

There must be a better way of doing this.

If website.save fails…I’m stuck with the new domain, thats not  
associated to a website.

def create  
website = Website.new(params[:website])  
website.domain = Domain.create(params[:domain])  
if website.save  
:flash[:notice] = Server.create\_website website  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

---

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [October 31, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/creating-two-activerecords-with-one-form/118850/2 "2007-10-31T18:27:55Z")

</div>

On 2007-10-31 09:40:20 -0700, “Chris.Mohr”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> There must be a better way of doing this.
> 
> If website.save fails…I’m stuck with the new domain, thats not  
> associated to a website.

This is a good tutorial on the subject:

> **[\#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

…also check out parts 2 and 3 of this lesson.

–Andrew

---

<div class="post-metadata">

**Author:** ![chrismalek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/6de8d8/32.png) [@chrismalek](https://www.ruby-forum.com/u/chrismalek)\
**Post date:** [November 1, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/creating-two-activerecords-with-one-form/118850/3 "2007-11-01T13:31:59Z")

</div>

For that you need to use transaction method so that if one save fails  
due to any reason then whole transaction should roll back. Something  
like this:

def create  
website = Website.new(params[:website])  
website.domain = Domain.create(params[:domain])  
begin  
Website.transaction do  
# the ! in save method is needed to raise any exceptions  
website.save!  
website.domain.save!  
end  
:flash[:notice] = Server.create\_website website  
redirect\_to :action =\> ‘list’  
rescue  
flash[:notice] = “Your error message”  
render :action =\> ‘new’  
end  
end

Hope that helps…

Also check [www.sphred.com](http://www.sphred.com)
