# Creating a child simultaneously with parent via form

**URL:** <https://www.ruby-forum.com/t/creating-a-child-simultaneously-with-parent-via-form/101842>\
**Category:** Rails\
**Created:** [August 8, 2006, 10:32am UTC](https://www.ruby-forum.com/t/creating-a-child-simultaneously-with-parent-via-form/101842 "2006-08-08T10:32:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [August 8, 2006, 10:32am UTC](https://www.ruby-forum.com/t/creating-a-child-simultaneously-with-parent-via-form/101842/1 "2006-08-08T10:32:09Z")

</div>

Hi there

I’ve got a user model and an address model; a user has\_many addresses.

A user object can be created via an html form which also allows for an  
initial address to be created.

I’ve got a controller method working to save both objects via the  
form, but I suspect I can achieve this in a more rails-esque manner.  
Here is my controller method:

def signup  
@user = User.new(params[:user])  
@address = Address.new(params[:address])  
return unless request.post?  
if @user.save  
@address.user\_id = @user.id  
if @address.save  
flash[:notice\_good] = “Your account needs to be now be  
activated. You have been sent a confirmation email with activation  
instructions.”  
redirect\_to :controller =\> “/shop”, :action =\> “index”  
end  
end  
end

Maybe this is OK, but any clues would be appreciated.

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [August 15, 2006, 2:11pm UTC](https://www.ruby-forum.com/t/creating-a-child-simultaneously-with-parent-via-form/101842/2 "2006-08-15T14:11:52Z")

</div>

Thanks Mark - that all makes sense.

It leads to another question however:

How to display validation errors for the address object along with  
those for the user object? At present I can only get “address can not  
be blank” to display, whereas I would like to see a field-by-field  
listing of errors as I get with user.

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [August 8, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/creating-a-child-simultaneously-with-parent-via-form/101842/3 "2006-08-08T17:12:05Z")

</div>

Richard S. wrote:

> @user = User.new(params[:user])  
> end  
> end
> 
> Maybe this is OK, but any clues would be appreciated.

What you’ve done is fine, but perhaps somewhat railier would be:

@user = User.new(params[:user])  
@address = @user.addresses.build(params[:address])  
if request.post? && @user.save  
flash[:notice\_good] = …  
redirect\_to :controller =\> :shop  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [August 15, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/creating-a-child-simultaneously-with-parent-via-form/101842/4 "2006-08-15T14:12:05Z")

</div>

Richard S. wrote:

> How to display validation errors for the address object along with  
> those for the user object? At present I can only get “address can not  
> be blank” to display, whereas I would like to see a field-by-field  
> listing of errors as I get with user.

Do you have “error\_messages\_for :address” in your view?

By the way, I was wrong when I said your original code was fine, because  
the user is saved even if the address doesn’t validate. With the code  
I gave Rails does automatic validation of the whole associated pair  
before any save is done.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![indigoi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b3f665/32.png) [@indigoi](https://www.ruby-forum.com/u/indigoi)\
**Post date:** [August 15, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/creating-a-child-simultaneously-with-parent-via-form/101842/5 "2006-08-15T14:25:19Z")

</div>

> Do you have “error\_messages\_for :address” in your view?

Yes, after using your code I can now display error\_messages\_for  
:address. Thank you 🙂

I’d like to consolidate them however with the messages for :user and  
suspect that involves hacking my own helper?

I’m also trying to functionally test the signup controller. In my  
functional test I have a create\_user method which gets called by my  
functional tests:

protected  
def create\_user(options = {})  
post :signup, :user =\>  
{  
:login =\> ‘quire’,  
:email =\> ‘removed\_email\_address@domain.invalid’,  
:password =\> ‘quire’,  
:password\_confirmation =\> ‘quire’,  
:first\_name =\> ‘quire’,  
:last\_name =\> ‘quire’  
}.merge(options)  
end

But since requiring that address fields are validated before a user  
can be saved, I need to ensure that dummy address attributes are added  
to my create\_user method above so that I can test the account  
controller methods properly.

This is where my lack of Ruby shows through…any clues appreciated.
