# Create a relationship with 2 or more tables on 1 insert

**URL:** <https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212>\
**Category:** Rails\
**Created:** [January 11, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212 "2006-01-11T23:17:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 11, 2006, 11:17pm UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/1 "2006-01-11T23:17:38Z")

</div>

userstable: |id|name|password|  
userdetailstable: |id|col1|col2|col3|user\_id|

this is my code:

```auto
def create
    @user = User.new(@params['user'])
    @userdetail = Userdetail.new(@params['userdetail'])

	    if @user.save and @userdetail.save
	    	flash[:notice] = "Save succeeded..."
	        redirect_to :action => 'list'
	    else
	    	flash[:notice] = "Save FAILED"
	  	render_action 'new'
	    end
	end
```

I want to put the auto\_incremented id of the first table(userstable)  
into the user\_id field of the second(userdetails) table

or is there a better way to do a multiple insert and at the same time  
create a relationship

thanx tim

---

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 12, 2006, 12:38am UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/2 "2006-01-12T00:38:31Z")

</div>

You should simply be able to do:  
@user = User.new params[:user]  
@user\_detail = UserDetail.new params[:userdetail]

@user.userdetail = @user\_detail

@user.save

And that should do it. Rails should take care of the rest. This should  
also work on has\_many relationships and such as well.

-Nick

---

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 12, 2006, 1:18am UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/3 "2006-01-12T01:18:10Z")

</div>

Tim van Oostrom wrote:

> ```
> if @user.save and @userdetail.save
> 
> ```
> 
> into the user\_id field of the second(userdetails) table
> 
> or is there a better way to do a multiple insert and at the same time  
> create a relationship

class User \< ActiveRecord::Base  
has\_one :details, :class\_name =\> ‘UserDetail’  
validates\_associated :details  
end

class UserDetail \< ActiveRecord::Base  
belongs\_to :user  
end

@user = User.new( params[‘user’] )  
@user.details = Userdetail.new( params[‘userdetail’] )  
if @user.save  
…

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

---

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 12, 2006, 1:30am UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/4 "2006-01-12T01:30:19Z")

</div>

> From the documentation:  
> [Peak Obsession](http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods).  
> html#M000472

@user = User.new( params[‘user’] )  
@user.create\_details( params[‘userdetail’] )  
if @user.save  
…

---

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 12, 2006, 2:25am UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/5 "2006-01-12T02:25:42Z")

</div>

Tom F. wrote:

> > From the documentation:  
> > [Peak Obsession](http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods).  
> > html#M000472
> 
> @user = User.new( params[‘user’] )  
> @user.create\_details( params[‘userdetail’] )  
> if @user.save  
> …

Ah it works now…

this line had to be:  
@user.create\_details( params[‘userdetail’] )

---

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 12, 2006, 2:27am UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/6 "2006-01-12T02:27:40Z")

</div>

Tom F. wrote:

> @user.create\_details( params[‘userdetail’] )

Yes, that is more concise than

> @user.details = Userdetail.new( params[‘userdetail’] )

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

---

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 12, 2006, 2:19am UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/7 "2006-01-12T02:19:09Z")

</div>

> I tried your solutions in various versions but i cant get it to work.  
> I did fix it myself but somehow i think it can be easier.

> mycode:

@user = User.new(params[:user])  
@user.save  
@userdetail = Userdetail.new(params[:userdetail])  
@userdetail.user\_id = @user.id

if @userdetail.save  
…

> Ok, maybe i dont understand it correct but here is how i think it is.  
> when i see this:

class User \< ActiveRecord::Base  
has\_one :userdetail  
end

class Userdetail \< ActiveRecord::Base  
belongs\_to :user  
end

> Does that mean that when i initiate a new object:

@user = User.new()

> The activerecord autmagically joins the user + userdetails objects together in one object called:

@user?

> and should that object also know the foreign key of user\_details = user\_id when

userstable: |id|name|password|  
userdetailstable: |id|col1|col2|col3|user\_id|  
(where user\_id is the foreign key of the id field on the userstable)

---

<div class="post-metadata">

**Author:** ![Tim\_van\_Oostrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Tim\_van\_Oostrom](https://www.ruby-forum.com/u/Tim_van_Oostrom)\
**Post date:** [January 12, 2006, 2:47am UTC](https://www.ruby-forum.com/t/create-a-relationship-with-2-or-more-tables-on-1-insert/52212/8 "2006-01-12T02:47:19Z")

</div>

fuck…  
i meant: \> @user.create\_userdetail( params[‘userdetail’] )
