Passing Model ID in Upload

Greetings.

I am trying to create an image upload page for my site, and I am having a little trouble figuring out how to set things up correctly. I have a model for user's, each with a unique ID, and I would like to have it so that when an image (i'm calling them artworks at the moment) is uploaded it is saved to /somedir/<userid>/ <imageid>.<extension> The trouble is I can't figure out how to pass the user ID info to rails so it will be saved in the image model. My code looks something like this:

# artwork migration   def self.up     create_table :artworks do |t|       t.column :title, :string       t.column :media, :string       t.column :description, :string       t.column :user_id, :integer # foreign key to users table       t.column :uploaded_at, :datetime       t.column :file_path, :string       t.column :file_name, :string       t.column :content_type, :string     end   end

# upload template <h1>Upload an artwork!</h1>

<% form_for(:artwork,         :url => { :action => :upload },         :html => { :multipart => true }) do |form| %>   Title: <%= form.text_field("title", { :size => 20, :maxlength => 100 }) %><br />   Media: <%= form.text_field("media", { :size => 20, :maxlength => 100 }) %><br />   Description: <%= form.text_area("description", { :cols => 60, :rows => 40 })%><br />   Select File to Upload: <%= form.file_field("uploaded_file") %><br />

  <%= submit_tag("Upload") %> <% end %>

# controller   def upload     logger.debug("running upload")

    # Create an empty artwork to display in the form for GET/POST     logger.debug("make a new Artwork")     @artwork = Artwork.new(params[:artwork])

    # If something was submitted     logger.debug("check for POST")     if request.post?       # If the save is successful, redisplay the page with a blank artwork for next submit       logger.debug("save to DB")       if @artwork.save         @artwork = Artwork.new         flash[:notice] = "Artwork Uploaded Successfully"       end     end   end

# Model for Artwork class Artwork < ActiveRecord::Base     belongs_to :user

    def uploaded_file=(incomming_file)       logger.debug("running uploaded_file=")

      @temp_file = incomming_file       @content_type = incomming_file.content_type.chomp       @file_path = MEDIA_FOLDER + @user_id + '/' # **** error here       @file_name = @id + "." + @content_type     end

    def after_save # called after a successful ActiveRecord save       if @temp_file         logger.debug("creating dir (if needed) '#{@file_path}'")         File.makedirs @file_path         logger.debug("saveing to file '#{@file_name}'")         File.open("#{@file_path}#{file_name}", "wb+") do |f|           f.write(@temp_file.read)         end       end     end

    def after_destroy       if File.exist?("#{@file_path}#{file_name}")         File.delete("#{@file_path}#{file_name}")       end     end end

When I run this code, I get an error at the line in the model above that @user_id is nil and can't converted to a String. I know I need to somehow get the current user's ID stored in the new artwork object, but I can't figure out how. I have it stored in the session hash when a user is logged in. I tried putting a hidden filed in the template like this:

<%= form.hidden_field("user_id", { :value => "#{@current_user_id}" }) %>

Where @current_user_id is grabs the id from the session, but same error.

I tried assigning the value in the controller just before calling @artwork = Artwork.new(params[:artwork]), but this seems to overwrite that value.

I tried accessing the session from within the model, but it looks like it's not in scope for that code. Any help would be appreciated.

Thanks, Matthew