Forum: Ruby on Rails file_column with polymorphic table relationship?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7f7702255eb1176a7bb10d4c9182931e?d=identicon&s=25 Daniel Naab (Guest)
on 2006-04-29 01:30
(Received via mailing list)
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
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-29 10:09
(Received via mailing list)
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.
7f7702255eb1176a7bb10d4c9182931e?d=identicon&s=25 Daniel Naab (Guest)
on 2006-05-03 19:09
(Received via mailing list)
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
This topic is locked and can not be replied to.