Forum: Ruby on Rails newbie q about saving data to mysql

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.
Vince W. (Guest)
on 2006-04-21 22:55
I have a two tables in my DB: users and products and I tried to set up a
foreign key relationship between the id field in user and a user_id
field in product.

Despite redoing my scaffold the user_id does not show up when I go to
create a new user.  Is that normal?

More importantly, how can I save the user id of the logged in user to
the user_id field in my product table?  Currently my create code looks
like this:

 def create
    warn @user.id
    @product = Product.new(params[:product])
    @product.id = @user.id
    if @product.save
      flash[:notice] = 'Product was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end


PS: @warn.id is shows the correct ID of the logged in user so at least
that works..
Vince W. (Guest)
on 2006-04-22 00:23
Ok, solved it.

Dumb mistake.  Needed to add this:

     @product.user_id = @user.id
Tom M. (Guest)
on 2006-04-22 04:58
(Received via mailing list)
You should also be able to say:

@product.user = @user

Which is more idiomatic rails.

--
-- Tom M.
This topic is locked and can not be replied to.