# Manipulating/Accessing objects that are :polymorphic =\> true

**URL:** <https://www.ruby-forum.com/t/manipulating-accessing-objects-that-are-polymorphic-true/151807>\
**Category:** Rails\
**Created:** [October 26, 2008, 1:52am UTC](https://www.ruby-forum.com/t/manipulating-accessing-objects-that-are-polymorphic-true/151807 "2008-10-26T01:52:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [October 26, 2008, 1:52am UTC](https://www.ruby-forum.com/t/manipulating-accessing-objects-that-are-polymorphic-true/151807/1 "2008-10-26T01:52:09Z")

</div>

I am working on an application to track warranty registrations for my  
employers product line. The product line includes the following  
products:

spreaders  
snow\_plows  
skid\_steers

Each of these products have different information that I have to track.  
The tables would look something line this:

```
create_table :spreaders do |t|
  t.integer :registration_id
  t.string :spreader_sn
  t.string :spreader_model_number

create_table :snow_plows do |t|
  t.integer :registration_id
  t.string :power_pack_sn
  t.string :control_package_sn
  t.string :blade_sn

create_table :skid_steers do |t|
  t.integer :registration_id
  t.string :plow_sn
  t.string :plow_control_sn
  t.string :blade_sn

```

Notice how each of these products need to collect different information.  
They are all registrations so I can create table to hold these  
registrations with information that is common to them all.

```
create_table :registrations do |t|
  t.integer :customer_id
  t.integer :dealer_id
  t.date :date_purchased
  t.integer :vehicle_id
  t.text :comment
  t.integer :registerable_id
  t.string :registerable_type

```

I have the models defined like so:

```
class Registration < ActiveRecord::Base
  belongs_to :customer
  belongs_to :dealer
  belongs_to :vehicle
  belongs_to :registerable, :polymorphic => true
end

class Spreader < ActiveRecord::Base
  has_many :registrations, :as => :registerable
end

class SnowPlow < ActiveRecord::Base
  has_many :registrations, :as => :registerable
end

class SkidSteer < ActiveRecord::Base
  has_many :registrations, :as => :registerable
end

```

At this point I am trying to simply display a record on the registration  
view and can’t seem to understand how I would access this. Here’s my  
registration view:

1

# Listing registrations
  
2  
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13  
14 \<% for registration in @registrations %\>  
15   
16   
17   
18   
19   
22   
23   
24 \<% end %\>  
25 

| Customer Name | Dealer Name | Date purchased | Product | Product SN | Vehicle | Comment |
| --- | --- | --- | --- | --- | --- | --- |
| \<%=h registration.customer.full\_name %\> | \<%=h registration.dealer.dealer\_name %\> | \<%=h registration.date\_purchased %\> | \<%=h registration.registerable\_type %\>  
20 | \<%= registration.registerable\_type.id %\>  
21 | \<%=h registration.vehicle.model %\> | \<%=h registration.comment %\> |

  
26  
27 \<%= link\_to ‘New Registration’, new\_registration\_path %\>

On lines 16, 17 and 21, I am able to display the full\_name of the  
Customer, likely because my Registration belongs\_to the Customer. I am  
also able to see the dealer\_name of the Dealer, similarly because my  
Registration belongs\_to the Dealer. And finally the model of the  
Vehicle, again because of the magical belongs\_to declaration.  
Wonderful. On line 20 is where I loose my mind.

I’m thinking there should be some similar magic going on behind the  
scenes to view the product (Spreader, SnowPlow, or SkidSteer) of the  
type listed in the registerable\_type field because of my nifty:  
belongs\_to :registerable, :polymorphic =\> true  
declaration in the Registration model. How would I access this record?  
It seems to me this functionality needs to be customized in the  
RegistrationsController because unlike the simple foreign key  
relationship Customers and Dealers enjoy with Registrations Rails does  
not know how to handle this polymorphic relationship by default. Or,  
which is probably more likely, I have no idea what I’m talking about and  
Rails DOES magically pull in the correct table because of that  
association. Ideas?

I can understand how to do this if I were to do this entirely  
differently by changing my design. For instance creating three new  
models called SpreaderRegistration, SnowPlowRegistration and  
SkidSteerRegistration and just throwing the whole idea of polymorphic  
relationships out the window. But it introduces duplication and I would  
have to maintain three separate MVC’s for each. Seems silly and not at  
all extensible. What if I have to add a new product that needs to be  
registered? Not I good idea I think.

There are other considerations I need to think about as well; such as  
how would I tell the Registration it is registerable\_type Spreader so it  
can create a new registration form that loads in the fields I need for  
the Spreader as well as the fields for all registrations. I am getting  
ahead of myself.

This MUST be a common problem. Can someone point me to a good tutorial  
that deals with this? I’ve found others that talk about polymorphic  
types and give migration and model examples, but nothing for the view or  
controller.

Thanks for your thoughts and time.

---

<div class="post-metadata">

**Author:** ![mrkohrea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@mrkohrea](https://www.ruby-forum.com/u/mrkohrea)\
**Post date:** [May 25, 2010, 10:19am UTC](https://www.ruby-forum.com/t/manipulating-accessing-objects-that-are-polymorphic-true/151807/2 "2010-05-25T10:19:09Z")

</div>

I am having the same problem in relation to your last point:

“There are other considerations I need to think about as well; such as  
how would I tell the Registration it is registerable\_type Spreader so it  
can create a new registration form that loads in the fields I need for  
the Spreader as well as the fields for all registrations. I am getting  
ahead of myself.”

Did you work out a solution for this? here is the issue I am having :

I have created a polymorphic association with Assets being the main  
class and article, image and offers being the sub classes.

I am trying to work out what is the best way to create individual asset  
types (article, image or offer) and pass this request through to the  
controller. Do I specify the asset to be created in the request url  
e.g. new/image or do ? What other methods can I use

At present I hard code the asset type in the new and create controller  
for Asset.

I am fine with editing or showing already created assets, as i can  
determine the asset type by querying the .class value of the asset.

Any help would be appreciated.
