# Polymorphic Associations

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations/195561>\
**Category:** Rails\
**Created:** [September 25, 2010, 5:47am UTC](https://www.ruby-forum.com/t/polymorphic-associations/195561 "2010-09-25T05:47:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![orientfang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a88e57/32.png) [@orientfang](https://www.ruby-forum.com/u/orientfang)\
**Post date:** [September 25, 2010, 5:47am UTC](https://www.ruby-forum.com/t/polymorphic-associations/195561/1 "2010-09-25T05:47:08Z")

</div>

Hi,  
I’m following the Polymorphic Associations testï¼Œ issue as following.

* * *

## class Product \< ActiveRecord::Base has\_many :images, :as=\>:imageable end

## class User \< ActiveRecord::Base has\_many :images, :as=\>:imageable end

class Image \< ActiveRecord::Base  
belongs\_to :imageable, :polymorphic =\> true  
end

\*\*migration:

class CreateImages \< ActiveRecord::Migration  
def self.up  
create\_table :images do |t|  
t.integer :imageable\_id  
t.string :imageable\_type  
t.string :name

```
  t.timestamps
end

```

end

def self.down  
drop\_table :images  
end  
end

\*\*rending views/images/show.html.erb

**OwnerName:** \<%= @image.imageable%\>

\*\*\*\*\* when i rendering the http://localhost:3000/images/1, the wrong message as following: \*\*\*\*\* NameError in Images#show

Showing /home/orient/rails-app/temp1/app/views/images/show.html.erb  
where line #20 raised:

wrong constant name user  
Extracted source (around line #20):

17:  
18:

19: **OwnerName:**  
20: \<%= @image.imageable%\>  
21:  
22:

  
23:
* * *

Any one could help , thanks in advance!!

---

<div class="post-metadata">

**Author:** ![orientfang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a88e57/32.png) [@orientfang](https://www.ruby-forum.com/u/orientfang)\
**Post date:** [September 27, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/polymorphic-associations/195561/2 "2010-09-27T16:39:20Z")

</div>

On Sep 24, 10:47Â pm, Orient F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> * * *

I think @image.imageable returns an ActiveRecord Object, which you  
can’t just display. If you are just testing:

@image.imageable.inspect should dump the active record object the  
image is related to.

If you are trying to get an attribute of the imageable relation, since  
there can be different relations you have to use the imageable\_type to  
make sure it is there and use something like @image.imageble.name

---

<div class="post-metadata">

**Author:** ![orientfang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a88e57/32.png) [@orientfang](https://www.ruby-forum.com/u/orientfang)\
**Post date:** [September 27, 2010, 5:42pm UTC](https://www.ruby-forum.com/t/polymorphic-associations/195561/3 "2010-09-27T17:42:35Z")

</div>

But does the image an imageable? how are you saving the image?

---

<div class="post-metadata">

**Author:** ![orientfang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/a88e57/32.png) [@orientfang](https://www.ruby-forum.com/u/orientfang)\
**Post date:** [September 27, 2010, 7:46pm UTC](https://www.ruby-forum.com/t/polymorphic-associations/195561/4 "2010-09-27T19:46:41Z")

</div>

On Sep 27, 10:41 am, radhames brito [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But does the image an imageable? how are you saving the image?

I was just talking about polymorphic in general. For images I’d use  
something like paperclip.

> **[GitHub - thoughtbot/paperclip: Easy file attachment management for ActiveRecord](https://github.com/thoughtbot/paperclip)**
>
> Easy file attachment management for ActiveRecord. Contribute to thoughtbot/paperclip development by creating an account on GitHub.

Then you’d have something like

class Image \< ActiveRecord::Base  
include Paperclip  
belongs\_to :imageable, :polymorphic =\> true  
has\_attached\_file :photo,  
:styles =\> {  
:thumb=\> “100x100”,  
:small =\> “200x200\>”,  
:medium =\> “300x300\>”,  
:large =\> “400x400\>” }

end
