File_column and has_many

I’ve been fighting with myself over this for the past week, and would
like to hear some others’ thoughts on the subject. I essentially have
the following models in place:

class Item < ActiveRecord::Base
has_many :images

class Image < ActiveRecord::Base
belongs_to :item
file_column :filename

In my view, I have javascript that basically creates N
file_column_fields for uploading multiple images. The problem I’m facing
is in the controller. What is the best method for getting all of these
files through the has_many and into file_column? I’ve tried a number of
variations on to no avail.

I know someone out there on the interweb has been through this before,
but the docs for file_column are terrible and the author doesn’t answer
his email (despite my desperate jumping through his anti-spam hoops).

On Fri, 2006-07-14 at 23:48 +0200, Brandon Mitchell wrote:

file_column :filename
his email (despite my desperate jumping through his anti-spam hoops).

this wouldn’t seem to be a problem with file_column or with has_many but
rather a problem of programming and logic.

You probably need to create an array or a hash in your view and return
that to your code with then iterates through the returned values and
saves them.

for each params[:image][:file] do
@image =
@image.item_id = params[:item_id]
@image.filename = params[:image][:file] = @image

you probably have to add a bunch of checking and a flash notice, etc.
but that is the general structure of what I am thinking and it’s hot and
I’m tired



I think you’re over-engineering this. All you really need to do is look
how other sites do this… a good example is gmail’s attachment system.

Top half of the screen has the file form. It contains a field to upload
box. It also contains a hidden form field that contains the item_id

<%=file_column_field “image”, “filename” %>
<input type=“hidden” name=“image[item_id]” value = <%[email protected] %>
<%=submit_tag “Upload Image” %>

The bottom half of the screen renders a partial that lists all files for

Submit the file… re-display this page again. Highlight the one you
added. None of this requires JavaScript and it’s actually very
plus it’s a lot easier to code.

I may be one of the few people that do things this way, but I will
ALWAYS just assign the foreign key as opposed to creating an unnecessary
object instance just so I can use << to associate the child element.
easier and faster just to assign the foreign key directly. You already
what item you’re attaching the files to anyway.

Would that solution work for you, or am I missing your intentions?

Brian H. wrote:

I think you’re over-engineering this.

I agree. That’s why I posted here for a reality check. :wink:

However, due to restrictions in the flow of the site itself, I need to
upload and associate the images with the Item upon initial creation. I
was hoping that I could make some magic happen in the models such that
in Item#create I would simply be doing an iterative <<, but that
preference is purely stylistic at this point. In that veign, here is how
I had envisioned the flow of the creation of a new Item:

Item#create receives params[] from view.
Within the Item model’s #new, any images in params[:images] are added
@params[:images].each do |i|
@item.images <<[i]

This would leave Item#create with one line to create a new item with all
associated images:
@item =[params[:item]]
Where params[:item][:images] is populated dynamically based upon the
quanitity of upload fields the user chooses.

Is this too much logic in the model? Given the requirement for adding N
images at Item#create, how would you do it?

I would do it just as I stated prevously. On a real estate site, I
the user to create a property and add photos at the same time… using
method I outlined above. They add them one at a time, so I don’t have
ask them how many they plan to add. It works out well, it’s simple to
implement, and the process is easy. As I said, I don’t like looping and
associationg with << because it’s not necessary here. YOu have the
key… just do Image.create params[:image], making sure that your image
array contains the item_id column.

Lots of luck to you if you really can’t do it this way. You can get
file_column to do multiple images on the same page but it’s more

Brian H. wrote:

Lots of luck to you if you really can’t do it this way. You can get
file_column to do multiple images on the same page but it’s more

Agreed. That’s what I’ve been experiencing. I certainly appreciate the
advice, and will give it a shot. Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs