# Using a legacy table with a column named 'type'

**URL:** <https://www.ruby-forum.com/t/using-a-legacy-table-with-a-column-named-type/120346>\
**Category:** Rails\
**Created:** [November 15, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/using-a-legacy-table-with-a-column-named-type/120346 "2007-11-15T23:42:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [November 15, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/using-a-legacy-table-with-a-column-named-type/120346/1 "2007-11-15T23:42:24Z")

</div>

I have a legacy database I use to pull data from and one of the  
tables has a column named ‘type’.

This cause the following sql to be generated (which doesn’t work):

SELECT \* FROM technical\_hint WHERE ( (technical\_hint.`type` =  
‘TechnicalHint’ ) )

If I change the name of the column in the legacy db form ‘type’ to  
typex’. It works fine and this is what the query looks like:

SELECT \* FROM technical\_hint

Anybody know waht’s happening and how I can get AR to consider this a  
‘non-magic’ name?

Thanks

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [November 16, 2007, 12:33am UTC](https://www.ruby-forum.com/t/using-a-legacy-table-with-a-column-named-type/120346/2 "2007-11-16T00:33:51Z")

</div>

> SELECT \* FROM technical\_hint
> 
> Anybody know waht’s happening and how I can get AR to consider this a  
> ‘non-magic’ name?

You want the ‘inheritance\_column’. There’s an AR method  
‘set\_inheritance\_column’ that you can change it to something else.

---

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [November 16, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/using-a-legacy-table-with-a-column-named-type/120346/3 "2007-11-16T15:51:08Z")

</div>

At 5:51 PM -0800 11/15/07, Philip H. wrote:

> You want the ‘inheritance\_column’. There’s an AR method  
> ‘set\_inheritance\_column’ that you can change it to something else.

Thanks Phillip,

I was able to do that in my model class for the legacy table with this  
statement:

self.inheritance\_column = “type\_id”

there is no column named “type\_id” in my legacy table and because of  
this AR doesn’t try any Single Table Inheritance magic when I access it.

For anybody interested I keep the models I use for connecting to a  
legacy database in a folder in app/models and the models in that folder  
use an abstract connection model for communications with the legacy  
database.

Assuming database.yml has a setting for connecting to a database called:  
‘legacy\_db’ here’s how I’d implement a model for using the table  
‘technical\_hint’ in the legacy database". The legacy technical\_hint  
table also has many technical\_hint\_implementations and the example below  
shows how I create the association.

file app/models/legacy/connection\_model.rb:

class Legacy::ConnectionModel \< ActiveRecord::Base  
def self.connection\_possible?  
begin  
find(:first)  
true  
rescue SystemCallError, SocketError  
false  
end  
end  
establish\_connection :legacy\_db  
end

file: app/models/legacy/technical\_hint.rb:

class Legacy::TechnicalHint \< Legacy::ConnectionModel  
set\_table\_name “technical\_hint”  
set\_primary\_key “technical\_hint\_id”  
has\_many :technical\_hint\_implementations,  
:class\_name =\> “Teemss2::TechnicalHintImplementation”,  
:foreign\_key =\> “technical\_hint\_id”  
self.inheritance\_column = “type\_id”  
end

In the example above the technical\_hint table also has a ‘type’ column  
which I need to tell AR to ignore by telling AR to use a non-existent  
column name for the the column it uses to do Single Table Inheritance  
magic.

This model is referenced like this in an application.

hints = Legacy::TechnicalHint.find(:all);

Implementing the models this way allows me to:

1. keep all the implementation details about a legacy database well  
scoped.
2. lets me create first classRrails TechnicalHint model in my  
application which won’t conflict with the model from the legacy table.

for which I am only using the legacy model
