You are not logged in.

#1 2006-04-21 19:43:40

viniosity
Member
From: Washington, DC
Registered: 2005-01-22
Posts: 377
Website

Rails scaffold and foreign keys

I set up a field in my products table called user_id where  hope to track the user associated with a particular product.

I regenerated my scaffold but that user_id field is not showing up. 

That in itself is not the big deal, but I'm tryinto figure out how to save the id of the user to that user_id field.

I can grab the correct id in the @user.id variable but can't figure out how to save it.

The complete section of the code (based on the agiles example) is here:

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

If anyone has any suggestions.. I'm a stuck little newbie at the moment..

Offline

#2 2006-04-21 20:13:40

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: Rails scaffold and foreign keys

To get this clear, are you trying to keep track of which administrative user created a new product by grabbing the id of whatever user is logged in?  My first guess would be that the problem with your controller code is that you just didn't have the right attribute for the product variable:

@product.user_id = @user.id

instead of

@product.id = @user_id

Although I'm not exactly sure how you're setting the @user instance variable...wouldn't the proper way be to grab the user id directly from the session? (assuming you've stuck pretty closely to the Agile's Depot application):

@product.user_id = session[:user_id]

Offline

#3 2006-04-21 20:20:23

viniosity
Member
From: Washington, DC
Registered: 2005-01-22
Posts: 377
Website

Re: Rails scaffold and foreign keys

I could have sworn I had tried that...

Anyway, that works.. thanks for the help!

Offline

#4 2006-04-21 22:17:16

elasticdog
Member
From: Washington, USA
Registered: 2005-05-02
Posts: 995
Website

Re: Rails scaffold and foreign keys

No problem...glad you got it figured out.

Offline

Board footer

Powered by FluxBB