Forum: Ruby on Rails new user.presentation

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.
Daan (Guest)
on 2006-03-18 13:22
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
Agnieszka F. (Guest)
on 2006-03-18 14:38
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


I think this should be @user.presentation = Presentation.new

--
Agnieszka F.
Howard R. (Guest)
on 2006-03-18 15:53
(Received via mailing list)
On 3/18/06, Daan <removed_email_address@domain.invalid> 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
joey__ (Guest)
on 2006-03-18 16:29
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
> end


joey__
http://www.feedreed.com
Daan (Guest)
on 2006-03-18 16:38
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
This topic is locked and can not be replied to.