# Polymorphic Associations in Rails 2 not configurable

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400>\
**Category:** Rails\
**Created:** [October 8, 2008, 4:07am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400 "2008-10-08T04:07:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 8, 2008, 4:07am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400/1 "2008-10-08T04:07:02Z")

</div>

This has been bugging me for some time.

The implementation for polymorphic associations in Rails is _still_  
using the record\_type, record\_id model where record type has to be the  
entire written out class name.

All of this logic is hardcoded deep in ActiveRecord, making it very  
difficult to deviate from this.

For example, what if I wanted to have the “type” column be a tinyint  
unsigned that mapped with the name of the class in some hash? Or what if  
I wanted to infer the type from a different column all together?

Repeating a long classname for every row in a database is repetitive and  
can eat up database size. For example, “SomeLongModuleName::ModelName”  
could be replaced with “1”, which would then map to that name. That  
would be 1 byte for the TinyInt(3) implementation vs N bytes for  
actually spelling out the class name _every_ time.

Whats frustrating me is the inability to easily configure this, or even  
to patch this to get it to work properly. Is anyone also having issues  
with this or is there anything out there to give you more control over  
polymorphic associations?

Aryk G.

> **[Mixbook | Photo Books, Cards, Calendars, and Wall Art](https://www.mixbook.com/)**
>
> Easily Create Photo Books, Scrapbooks, Photo Cards, Yearbooks and Calendars in minutes with our simple online scrapbooking software. Mixbook - Make It Yours!

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 8, 2008, 4:19am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400/2 "2008-10-08T04:19:19Z")

</div>

You could just build your own associations solution instead of trying to  
patch the polymorphic associations.

Also, if you have indexes for the columns, the time “searching” for the  
row  
isn’t really different than it would be for a number instead of a  
varchar.  
The database should hit the result row in the same time.

On Tue, Oct 7, 2008 at 11:07 PM, Aryk G. \<  
removed\_email\_address@domain.invalid\> wrote:

> For example, what if I wanted to have the “type” column be a tinyint  
> to patch this to get it to work properly. Is anyone also having issues  
> with this or is there anything out there to give you more control over  
> polymorphic associations?
> 
> ## Aryk G. [Mixbook.com](http://Mixbook.com)
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
MaurÃ­cio Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
JoÃ£o Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 8, 2008, 4:38am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400/3 "2008-10-08T04:38:25Z")

</div>

Yes you could just build your own for this task or that task, but then  
you start adding function after function until you realize why  
activerecord made associations in the first place (getters, setters,  
callbacks, reflections). =)

I tried patching polymorphic associations, its not easy, because you  
have to change the behavior in like 10 different places. Its not pretty.

Im not worried about the time searching for the record, Im frustrated  
about using 30 bytes to store “ThisLongClassNameBlahBlah” vs 1 byte to  
store a number that maps to that name for every record.

Also, I’d have to add a “\_type” column even though you could infer the  
classname based off another column, thus making things anything but DRY.

MaurÃ­cio Linhares wrote:

> You could just build your own associations solution instead of trying to  
> patch the polymorphic associations.
> 
> Also, if you have indexes for the columns, the time “searching” for the  
> row  
> isn’t really different than it would be for a number instead of a  
> varchar.  
> The database should hit the result row in the same time.
> 
> On Tue, Oct 7, 2008 at 11:07 PM, Aryk G. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > For example, what if I wanted to have the “type” column be a tinyint  
> > to patch this to get it to work properly. Is anyone also having issues  
> > with this or is there anything out there to give you more control over  
> > polymorphic associations?
> > 
> > ## Aryk G. [Mixbook.com](http://Mixbook.com)
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> –  
> MaurÃ­cio Linhares  
> [http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
> (en)  
> JoÃ£o Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 8, 2008, 4:52am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400/4 "2008-10-08T04:52:40Z")

</div>

On Tue, Oct 7, 2008 at 11:38 PM, Aryk G. \<  
removed\_email\_address@domain.invalid\> wrote:

> Also, I’d have to add a “\_type” column even though you could infer the  
> classname based off another column, thus making things anything but DRY.

Could you explain this a little bit more?

I guess i don’t get the idea you’re trying to reach here. How could you  
infer the classname based on another column without having the classname  
in  
that other column?

–  
MaurÃ­cio Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
JoÃ£o Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 9, 2008, 4:04am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400/5 "2008-10-09T04:04:17Z")

</div>

Well, that’s definitely a “complicated” example 🙂

I guess you should try to figure out with the core rails guys where  
you could hack the code to do just that (what would be infer the name  
of the real association from something else), i haven’t looked at the  
code myself, but it shouldn’t be that hard with the right guidance.

Also, if you want to roll your own solution, it isn’t that hard and  
there’s also plenty of work already written about the matter, one of  
the best options is Martin F.'s “Patterns of Enterprise  
Application Architeture” (  
[Catalog of Patterns of Enterprise Application Architecture](http://martinfowler.com/eaaCatalog/index.html) ), there are 3 sections  
about object-relational mapping tool structure.

On Wed, Oct 8, 2008 at 1:30 AM, Aryk G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So, now I need a “detail\_type” column of “BookDetail” or in other words  
> “#{product\_type.classify}Detail”.
> 
> Now I have two extra string columns that are not needed and are making  
> things much less DRY.
> 
> Aryk

–  
Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)  
João Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 9, 2008, 4:16am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400/6 "2008-10-09T04:16:23Z")

</div>

Thanks for the input,

I wouldnt say this is exactly a complicated example. Its basically where  
you could infer the polymorphic class from STI type sort of thing. Im  
pretty sure many other people run into it.

The bottom line is that there is no customizablility.

The problem is that the code has sql strings generated with  
"#{variable}\_type = X which makes it very hard to replace with a  
function to control the outputted polymorphic sql condition.

I wonder if there is anybody else that has run into the same problems.

MaurÃ­cio Linhares wrote:

> Well, that’s definitely a “complicated” example 🙂
> 
> I guess you should try to figure out with the core rails guys where  
> you could hack the code to do just that (what would be infer the name  
> of the real association from something else), i haven’t looked at the  
> code myself, but it shouldn’t be that hard with the right guidance.
> 
> Also, if you want to roll your own solution, it isn’t that hard and  
> there’s also plenty of work already written about the matter, one of  
> the best options is Martin F.'s “Patterns of Enterprise  
> Application Architeture” (  
> [Catalog of Patterns of Enterprise Application Architecture](http://martinfowler.com/eaaCatalog/index.html) ), there are 3 sections  
> about object-relational mapping tool structure.
> 
> On Wed, Oct 8, 2008 at 1:30 AM, Aryk G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > So, now I need a “detail\_type” column of “BookDetail” or in other words  
> > “#{product\_type.classify}Detail”.
> > 
> > Now I have two extra string columns that are not needed and are making  
> > things much less DRY.
> > 
> > Aryk
> 
> –  
> Maurï¿½cio Linhares  
> [http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
> (en)  
> Joï¿½o Pessoa, PB, +55 83 8867-7208

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 8, 2008, 6:30am UTC](https://www.ruby-forum.com/t/polymorphic-associations-in-rails-2-not-configurable/150400/7 "2008-10-08T06:30:05Z")

</div>

Sure Mauricio, here’s an example.

Lets say you have a “products” table. You have product\_type table column  
in that table that you use for single table inheritance. So lets say you  
have the value “book” as the product\_type.

Now you want to have a pricing for this product, so you do:

belongs\_to :pricing, :polymorphic =\> true

So now you need a pricing\_type with a class name of “BookPricing”, or in  
other words “#{product\_type.classify}Pricing”. You will always follow  
this convention since I wouldnt have BookPricing for a product\_type of  
“poster”

But wait! There’s more. Now I want another polymorphic association for  
“details” for that product. So I do:

belongs\_to :detail, :polymorphic =\> true

So, now I need a “detail\_type” column of “BookDetail” or in other words  
“#{product\_type.classify}Detail”.

Now I have two extra string columns that are not needed and are making  
things much less DRY.

Aryk

MaurÃ­cio Linhares wrote:

> On Tue, Oct 7, 2008 at 11:38 PM, Aryk G. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > Also, I’d have to add a “\_type” column even though you could infer the  
> > classname based off another column, thus making things anything but DRY.
> 
> Could you explain this a little bit more?
> 
> I guess i don’t get the idea you’re trying to reach here. How could you  
> infer the classname based on another column without having the classname  
> in  
> that other column?
> 
> –  
> MaurÃ­cio Linhares  
> [http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
> (en)  
> JoÃ£o Pessoa, PB, +55 83 8867-7208
