# Defining polymorphic type condition

**URL:** <https://www.ruby-forum.com/t/defining-polymorphic-type-condition/149543>\
**Category:** Rails\
**Created:** [September 26, 2008, 1:23am UTC](https://www.ruby-forum.com/t/defining-polymorphic-type-condition/149543 "2008-09-26T01:23:52Z")\
**Posts on this page:** 2\
**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:** [September 26, 2008, 1:23am UTC](https://www.ruby-forum.com/t/defining-polymorphic-type-condition/149543/1 "2008-09-26T01:23:52Z")

</div>

As great as Rails 2.1 is, I for the life of me cannot figure out why  
polymorphic associations do not support reconfiguring the type column  
used.

For example, I have no way to do a polymorphic association using a  
lookup hash which maps an integer to a class name.

Storing a TINYINT unsigned is waaaaay more efficient than storing a  
string like “SomeActiveRecordModel” over and over again.

Looking in the association proxy classes for has\_one and has\_many, I  
see:

```
  def construct_sql
    case
    when @reflection.options[:as]
      @finder_sql =
        "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id

```

= #{@owner.quoted\_id} AND " +  
“#{@reflection.quoted\_table\_name}.#{@reflection.options[:as]}\_type  
= #{@owner.class.quote\_value(@owner.class.base\_class.name.to\_s)}”  
else  
@finder\_sql =  
“#{@reflection.quoted\_table\_name}.#{@reflection.primary\_key\_name} =  
#{@owner.quoted\_id}”  
end  
@finder\_sql \<\< " AND (#{conditions})" if conditions  
end

This cannot easily be changed. The type condition should be a function  
call so we could easily modified. Has anybody thought about this?

---

<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:** [September 26, 2008, 1:24am UTC](https://www.ruby-forum.com/t/defining-polymorphic-type-condition/149543/2 "2008-09-26T01:24:50Z")

</div>

Or better yet, any solutions/patches?
