Hi, I''m creating an RoR application right now and i''ve got the following question: An user has one presentation. So i''ve got an user table and in that table is an field fkPresentationID. Now i want to create an new user and at the same moment an new presentation for that user. I thought it would work with something like this, but it didn''t work. @user = User.new(params[:user]) @user.presentation.new(params[:presentation]) if @user.save flash[:notice] = ''OK.'' redirect_to :action => ''list'' else render :action => new end Can anyone tell me what i''m doing wrong? Thanks in advance -- Posted via http://www.ruby-forum.com/.
Daan wrote:> Hi, > > I''m creating an RoR application right now and i''ve got the following > question: > > An user has one presentation. > > So i''ve got an user table and in that table is an field > fkPresentationID. Now i want to create an new user and at the same > moment an new presentation for that user. I thought it would work with > something like this, but it didn''t work. > > @user = User.new(params[:user]) > @user.presentation.new(params[:presentation]) > if @user.save > flash[:notice] = ''OK.'' > redirect_to :action => ''list'' > else > render :action => new > end > > Can anyone tell me what i''m doing wrong? > > Thanks in advanceI think this should be @user.presentation = Presentation.new -- Agnieszka Figiel -- Posted via http://www.ruby-forum.com/.
On 3/18/06, Daan <daan@iwsweb.nl> wrote:> > Hi, > > I''m creating an RoR application right now and i''ve got the following > question: > > An user has one presentation. > > So i''ve got an user table and in that table is an field > fkPresentationID.Hi Daan, Not fully awake yet (still working on my first pot of coffee :) ...but I did want to point one thing out... You do know that the convention for this field is typically presentation_id ? If you need this field name, make sure you have this in your Presentation model: belongs_to :user, :foreign_key => "fkPresentationID" HTH, Dean -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/371adcf7/attachment.html
Daan wrote:> Hi, > > I''m creating an RoR application right now and i''ve got the following > question: > > An user has one presentation. > > So i''ve got an user table and in that table is an field > fkPresentationID. Now i want to create an new user and at the same > moment an new presentation for that user. I thought it would work with > something like this, but it didn''t work. > > @user = User.new(params[:user]) > @user.presentation.new(params[:presentation]) > if @user.save > flash[:notice] = ''OK.'' > redirect_to :action => ''list'' > else > render :action => new > end > > Can anyone tell me what i''m doing wrong? > > Thanks in advance@user needs to be saved before a presentation can be attched, becasue user needs to have a id for the presentation to recognize it.> @user = User.new(params[:user]) > if @user.save > @user.presentation.create(params[:presentation]) > flash[:notice] = ''OK.'' > redirect_to :action => ''list'' > else > render :action => new > endjoey__ http://www.feedreed.com -- Posted via http://www.ruby-forum.com/.
Thanks all, I works finaly :) i''m using the following code right now: @user = User.new(params[:user]) @presentation = Presentation.new(params[:presentation]) @user.valid? @presentation.valid? if @presentation.valid? and @user.valid? @presentation.save @user.presentation = @presentation @user.lastlogin = Time.now @user.save flash[:notice] = ''Ok'' redirect_to :action => ''list'' else render :action => ''new'' end -- Posted via http://www.ruby-forum.com/.