Monday, December 18, 2006

Using ActiveRecord Models in Migrations With STI

It's cool to use ActiveRecord models in rails migrations...


However you might come across a problem i just had....

If you have a model utilising STI, you may find (like I) that you get a big bad "superclass mismatch for class X". ActiveRecord is instantiating your model class based on the 'type' column in your STI table - even if you use the parent class.

I hacked a workaround by disabling STI in my migration:

# Disable Single Table Inheritance...
ActiveRecord::Base.inheritance_column = 'something_to_disable_single_table_inheritance'


This will mean it won't look for a column called 'type' but whatever else you say there - just make sure you don't have a column called 'something_to_disable_single_table_inheritance'

:0

1 comment:

Anonymous said...

Thanks for the tip. Works very well indeed!