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?