File_column with polymorphic table relationship?


#1

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


#2

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.


#3

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