Upload multi files using file_column


#1

Hi,

I have a Photo table like this

id
image_url
title

I hope I can use file_column to load image. The problem is file_column
only
supports one file. I know I can create multi columns in the table to
load multi files using
file_column but I only want one column and I want the files being
uploaded to save into different record instead of one photo record.

Is anyway I can define a virtual attribute like this?

class Photo < ActiveRecord
file_column :image_url #image_url will be a real
column
file_column :virtual_url_1 #this is not a table
column, just a holder for file
file_column :virtual_url_2 #this is not a table
column, just a holder for file

def virtual_url_1(value)
save the second file into a new photo record
end

def virtual_url_2(value)
save the third file into a new photo record
end

end

I tried this but it seems not working, could anyone give me
suggestions on how to solve this problem?

thanks/chong


#2

On 12/12/05, chongqing xiao removed_email_address@domain.invalid wrote:

uploaded to save into different record instead of one photo record.
how about just having a Photo model with one file_column and simply
storing several of them? If you have several file uploads field in
your view like this:

<%= file_column “photo1”, “image” %>
<%= file_column “photo2”, “image” %>

and in your controller that saves the form:

@photo1 = Photo.new(params[:photo1])
@photo1.save

@photo2 = Photo.new(params[:photo2])
@photo2.save

There should be a more elegant way with rails auto-numbering the
fields, but I’m too lazy to figure this out. Of course, you could loop
to avoid violating DRY etc. but this should give you an idea.

Sebastian