# File\_column and has\_many

**URL:** <https://www.ruby-forum.com/t/file-column-and-has-many/98473>\
**Category:** Rails\
**Created:** [July 14, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/file-column-and-has-many/98473 "2006-07-14T23:48:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ipaddict](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ipaddict](https://www.ruby-forum.com/u/ipaddict)\
**Post date:** [July 14, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/file-column-and-has-many/98473/1 "2006-07-14T23:48:17Z")

</div>

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  
end

class Image \< ActiveRecord::Base  
belongs\_to :item  
file\_column :filename  
end

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 Image.new 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).  
TIA.

---

<div class="post-metadata">

**Author:** ![ipaddict](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ipaddict](https://www.ruby-forum.com/u/ipaddict)\
**Post date:** [July 15, 2006, 1:36am UTC](https://www.ruby-forum.com/t/file-column-and-has-many/98473/2 "2006-07-15T01:36:55Z")

</div>

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).  
> TIA.

* * *

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.new  
@image.item\_id = params[:item\_id]  
@image.filename = params[:image][:file]  
Image.save = @image  
end

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

Craig

---

<div class="post-metadata">

**Author:** ![ipaddict](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ipaddict](https://www.ruby-forum.com/u/ipaddict)\
**Post date:** [July 15, 2006, 7:30am UTC](https://www.ruby-forum.com/t/file-column-and-has-many/98473/3 "2006-07-15T07:30:47Z")

</div>

Brandon:

I think you’re over-engineering this. All you really need to do is look  
at  
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  
the  
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 = \<%=@item.id %\>  
\<%=submit\_tag “Upload Image” %\>

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

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

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

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

---

<div class="post-metadata">

**Author:** ![ipaddict](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ipaddict](https://www.ruby-forum.com/u/ipaddict)\
**Post date:** [July 15, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/file-column-and-has-many/98473/4 "2006-07-15T18:24:04Z")

</div>

Brian H. wrote:

> I think you’re over-engineering this.

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

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  
like:  
@params[:images].each do |i|  
@item.images \<\< Image.new[i]  
end

This would leave Item#create with one line to create a new item with all  
associated images:  
@item = Item.new[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?

---

<div class="post-metadata">

**Author:** ![ipaddict](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ipaddict](https://www.ruby-forum.com/u/ipaddict)\
**Post date:** [July 15, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/file-column-and-has-many/98473/5 "2006-07-15T19:20:39Z")

</div>

I would do it just as I stated prevously. On a real estate site, I  
allow  
the user to create a property and add photos at the same time… using  
the  
method I outlined above. They add them one at a time, so I don’t have  
to  
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  
foreign  
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  
difficult.

---

<div class="post-metadata">

**Author:** ![ipaddict](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/858c86/32.png) [@ipaddict](https://www.ruby-forum.com/u/ipaddict)\
**Post date:** [July 15, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/file-column-and-has-many/98473/6 "2006-07-15T19:32:33Z")

</div>

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  
> difficult.

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