What i do if i have a table with a column named "type"?

Hi everybody!

The class ActiveRecord::Base have a atribute named type and if my
table have a column with the same name i get an error if i use
model.finde, model.save and more…

I dont know what i can do to solve this problem!

Someone know?

Thanks

Noel R. Morais wrote:

The class ActiveRecord::Base have a atribute named type and if my
table have a column with the same name i get an error if i use
model.finde, model.save and more…

I dont know what i can do to solve this problem!

http://rubyonrails.org/api/classes/ActiveRecord/Base.html#M000879

Put this in your model class:

def inheritance_column
“something_other_than_type”
end


Josh S.
http://blog.hasmanythrough.com

Hi Noel,

Noel R. Morais

The class ActiveRecord::Base have a atribute named type and if my table
have a column with the same name i get an error if i use
model.finde, model.save and more…

I dont know what i can do to solve this problem!

You cannot have a column named ‘type’ and use RoR. It is a Magic Field
Name
( Peak Obsession )

From that page…
“Rails values convention over configuration. This is also true in the
realm
of table design where fields given particular names automatically gain
certain behaviours.”

hth,
Bill

On 1-Jun-06, at 2:26 PM, Josh S. wrote:

def inheritance_column
“something_other_than_type”
end

Sorry but that’s not the way to go. The inheritance_column method is
a class method.

Regardless, I believe the correct call is set_inheritance_column. I.e.:

class MyModel < ActiveRecord::Base
set_inheritance_column :ruby_type
end

However, if you want to access the value for the ‘type’ field in your
table you can’t do:

model.type

you have to use:

model[:type]

All in all, if you can avoid it then don’t have a column called
“type”. I don’t even use it for STI - a while back I picked up a
habit of always setting the inheritance column to be ‘ruby_type’.

That way I can do model.type() to get back the class object and
model.ruby_type (and model.ruby_type=) to access the string
representation of the class name.

HTH,
Trevor

Trevor S.
http://somethinglearned.com

Trevor S. wrote:

On 1-Jun-06, at 2:26 PM, Josh S. wrote:

def inheritance_column
“something_other_than_type”
end

Sorry but that’s not the way to go. The inheritance_column method is
a class method.

Regardless, I believe the correct call is set_inheritance_column. I.e.:

class MyModel < ActiveRecord::Base
set_inheritance_column :ruby_type
end

However, if you want to access the value for the ‘type’ field in your
table you can’t do:

model.type

you have to use:

model[:type]

All in all, if you can avoid it then don’t have a column called
“type”. I don’t even use it for STI - a while back I picked up a
habit of always setting the inheritance column to be ‘ruby_type’.

That way I can do model.type() to get back the class object and
model.ruby_type (and model.ruby_type=) to access the string
representation of the class name.

Thanks, Trevor. That’s a great explanation. I was just going off the
docs, as I never have needed a field named “type” myself. You up for
fixing the docs for #inheritance_column that say it can be overridden in
a sublcass? :slight_smile:

I hear that “type” is eventually going to stop being a magic word in
Ruby. Kinnehora!


Josh S.
http://blog.hasmanythrough.com