Using a legacy table with a column named 'type'

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

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.

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