Forum: Ruby on Rails Create a relationship with 2 or more tables on 1 insert

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tim van Oostrom (Guest)
on 2006-01-12 00:17
userstable: |id|name|password|
userdetailstable: |id|col1|col2|col3|user_id|

this is my code:

[code]
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
[/code]

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
Nick S. (Guest)
on 2006-01-12 01:38
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2006-01-12 02:18
(Received via mailing list)
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.
Tom F. (Guest)
on 2006-01-12 02:30
(Received via mailing list)
>From the documentation:
http://rails.rubyonrails.com/classes/ActiveRecord/....
html#M000472

  @user = User.new( params['user'] )
  @user.create_details( params['userdetail'] )
  if @user.save
    ...
Tim van Oostrom (Guest)
on 2006-01-12 03:19
> 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)
Tim van Oostrom (Guest)
on 2006-01-12 03:25
Tom F. wrote:
>>From the documentation:
> http://rails.rubyonrails.com/classes/ActiveRecord/....
> 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'] )
Mark Reginald J. (Guest)
on 2006-01-12 03:27
(Received via mailing list)
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.
Tim V. (Guest)
on 2006-01-12 03:47
fuck..
i meant: > @user.create_userdetail( params['userdetail'] )
This topic is locked and can not be replied to.