legacy database with column named 'type'

I have a problem with a table that has a field named 'type'

When I try to use active record in a console, I get this error...

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: '10'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Debtortrans.inheritance_column to use another column for that information.

Since I can't rename the column because the software creating it needs this, is it possible to alias the name somehow within rails, perhaps in the model?

Craig

The default value for the inheritance_column is "type" so you just need to change that to something else and then you should be able to use 'type' normally for your legacy data.

class Debtortrans    inheritance_column :not_used end

(Assuming that there isn't a column named "not_used" in that table, of course. :wink:

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Is there another way using some sort of alias on the column name as the OP suggested? I have a legacy db with a column called action and suspect that this may cause me some problems at some point. Maybe it will not however.

Colin

> > I have a problem with a table that has a field named 'type' > > When I try to use active record in a console, I get this error... > > ActiveRecord::SubclassNotFound: The single-table inheritance mechanism > failed to locate the subclass: '10'. This error is raised because the > column 'type' is reserved for storing the class in case of
> inheritance. > Please rename this column if you didn't intend it to be used for
> storing > the inheritance class or overwrite Debtortrans.inheritance_column to
> use > another column for that information. > > Since I can't rename the column because the software creating it needs > this, is it possible to alias the name somehow within rails, perhaps
> in > the model? > > Craig

The default value for the inheritance_column is "type" so you just
need to change that to something else and then you should be able to
use 'type' normally for your legacy data.

class Debtortrans    inheritance_column :not_used end

(Assuming that there isn't a column named "not_used" in that table, of
course. :wink:

If it's because you see "action" in this page:

http://wiki.rubyonrails.org/rails/pages/ReservedWords

(or a similar one), then it is probably within a controller. You shouldn't have problems with 'action' as a column/attribute name on a model.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

I admit I have not run into a problem yet, I suppose I am concerned that something like :conditions => {:action => value} might confuse the system. I suppose if it did I could avoid the use of :action by using ['action = ?', value] instead.

I will not worry unless it becomes a problem. Thanks for the help.

Colin

I had a similar problem when I hooked my Wordpress installation into my rails site. On the wp_posts table they use ID as their primary key. Ruby/Rails was getting unhappy whenever I tried to access obj.ID. I was able to access this field by using obj.attributes ["ID"].

I don't know if this will work for you without a code example, but its worth a shot.

Inheritance_column issues - Rails - Ruby-Forum suggests that just changing the inheritance column name may not be enough, it may also be necessary to provide access methods as type is a reserved word. The link suggests a solution.

Colin

> > >> >> I have a problem with a table that has a field named 'type' >> >> When I try to use active record in a console, I get this error... >> >> ActiveRecord::SubclassNotFound: The single-table inheritance mechanism >> failed to locate the subclass: '10'. This error is raised because the >> column 'type' is reserved for storing the class in case of >> inheritance. >> Please rename this column if you didn't intend it to be used for >> storing >> the inheritance class or overwrite Debtortrans.inheritance_column to >> use >> another column for that information. >> >> Since I can't rename the column because the software creating it needs >> this, is it possible to alias the name somehow within rails, perhaps >> in >> the model? >> >> Craig > > > The default value for the inheritance_column is "type" so you just > need to change that to something else and then you should be able to > use 'type' normally for your legacy data. > > class Debtortrans > inheritance_column :not_used > end > > (Assuming that there isn't a column named "not_used" in that table, of > course. :wink: >

Inheritance_column issues - Rails - Ruby-Forum suggests that just changing the inheritance column name may not be enough, it may also be necessary to provide access methods as type is a reserved word. The link suggests a solution.