# File\_column with polymorphic table relationship?

**URL:** <https://www.ruby-forum.com/t/file-column-with-polymorphic-table-relationship/59142>\
**Category:** Rails\
**Created:** [April 29, 2006, 1:30am UTC](https://www.ruby-forum.com/t/file-column-with-polymorphic-table-relationship/59142 "2006-04-29T01:30:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Naab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6de8d8/32.png) [@Daniel\_Naab](https://www.ruby-forum.com/u/Daniel_Naab)\
**Post date:** [April 29, 2006, 1:30am UTC](https://www.ruby-forum.com/t/file-column-with-polymorphic-table-relationship/59142/1 "2006-04-29T01:30:49Z")

</div>

Hello,

I’m just starting out with both Rails and Ruby, so I hope this isn’t too  
simple of a question - but I’ve been struggling to find a solution.

I’m using the file\_column plugin to handle image uploads and thumbnail  
generation. In addition, I’ve wrapped the image into a polymorphic  
table  
so I can have uploaded images associated with multiple datatypes.  
However, I’m having trouble determining how to reference the file\_column  
field in the images table from an associated view.

As an example, here’s a condensed version of the relationships:

```
create_table :images do |t|
  t.column :file, :string
  t.column :illustrated_id, :integer
  t.column :illustrated_type, :string
end

create_table :thing do |table|
  table.column :name, :string
  table.column :description, :string
end

```

…and models:

class Thing \< ActiveRecord::Base  
has\_one :image, :as =\> :illustrated  
end

class Image \< ActiveRecord::Base  
belongs\_to :illustrated, :polymorphic =\> true  
file\_column :file, :magick =\> {  
:versions =\> { “thumb” =\> “100x100”, “medium” =\> “640x480\>” },  
:attributes =\> { :format =\> “JPG” }  
}  
end

Now, in an Image view - which in practice I don’t think I would use, but  
for the sake of demonstration - I can do something like this to get the  
uploads working without a problem:

\<%= file\_column\_field “image”, “file” %\>

But how would I do the same from the so-called “Thing” view? Or is it  
not  
possible to handle in the view without writing a model method? I’d  
prefer  
not to, as there would be several models with duplicated code.

Thank you!

Dan Naab

---

<div class="post-metadata">

**Author:** ![Daniel\_Naab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6de8d8/32.png) [@Daniel\_Naab](https://www.ruby-forum.com/u/Daniel_Naab)\
**Post date:** [April 29, 2006, 10:09am UTC](https://www.ruby-forum.com/t/file-column-with-polymorphic-table-relationship/59142/2 "2006-04-29T10:09:53Z")

</div>

Daniel, not sure if reading this correctly, but to display a photo that  
belongs to Thing you could do something like this:

In the controller:  
@image = @thing.image

In the view:  
\<%= image\_tag(url\_for\_file\_column(“image”, “file”)) if @image %\>

To upload is a bit more tricky (mainly on the controller side). If  
that’s what you’re wanting, post back, and I’ll show you how I did it.

---

<div class="post-metadata">

**Author:** ![Daniel\_Naab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/6de8d8/32.png) [@Daniel\_Naab](https://www.ruby-forum.com/u/Daniel_Naab)\
**Post date:** [May 3, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/file-column-with-polymorphic-table-relationship/59142/3 "2006-05-03T19:09:53Z")

</div>

On Sat, April 29, 2006 3:07 am, Chris T wrote:

> To upload is a bit more tricky (mainly on the controller side). If  
> that’s what you’re wanting, post back, and I’ll show you how I did it.

Sorry for not being more clear. I am indeed asking how to do an upload  
from the “Thing” view.

Thanks,  
Dan
