Tim van Oostrom
2006-Jan-11 22:17 UTC
[Rails] Create a relationship with 2 or more tables on 1 insert
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 -- Posted via http://www.ruby-forum.com/.
Nick Stuart
2006-Jan-11 23:36 UTC
[Rails] Create a relationship with 2 or more tables on 1 insert
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 On 1/11/06, Tim van Oostrom <timvanoostrom@gmail.com> wrote:> 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Mark Reginald James
2006-Jan-12 00:16 UTC
[Rails] Re: Create a relationship with 2 or more tables on 1 insert
Tim van Oostrom wrote:> 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 relationshipclass 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 Fakes
2006-Jan-12 00:28 UTC
[Rails] Re: Create a relationship with 2 or more tables on 1 insert
>From the documentation:http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods. html#M000472 @user = User.new( params[''user''] ) @user.create_details( params[''userdetail''] ) if @user.save ... -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Mark Reginald James Sent: Wednesday, January 11, 2006 4:16 PM To: rails@lists.rubyonrails.org Subject: [Rails] Re: Create a relationship with 2 or more tables on 1 insert Tim van Oostrom wrote:> 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 relationshipclass 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. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Tim van Oostrom
2006-Jan-12 01:19 UTC
[Rails] RE: Re: Create a relationship with 2 or more tables on 1 ins
> 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 whenuserstable: |id|name|password| userdetailstable: |id|col1|col2|col3|user_id| (where user_id is the foreign key of the id field on the userstable) -- Posted via http://www.ruby-forum.com/.
Tim van Oostrom
2006-Jan-12 01:25 UTC
[Rails] Re: Create a relationship with 2 or more tables on 1 ins
Tom Fakes wrote:>>From the documentation: > 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''] ) -- Posted via http://www.ruby-forum.com/.
Mark Reginald James
2006-Jan-12 01:26 UTC
[Rails] Re: Create a relationship with 2 or more tables on 1 insert
Tom Fakes 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.