Ruby Forum Ruby on Rails > What i do if i have a table with a column named "type"?

Posted by Noel R. Morais (Guest)
on 01.06.2006 22:57
(Received via mailing list)
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
Posted by Josh Susser (jsusser)
on 01.06.2006 23:26
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 Susser
http://blog.hasmanythrough.com
Posted by Bill Walton (Guest)
on 01.06.2006 23:38
(Received via mailing list)
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
( http://wiki.rubyonrails.com/rails/pages/MagicFieldNames )

>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
Posted by Trevor Squires (Guest)
on 02.06.2006 01:30
(Received via mailing list)
On 1-Jun-06, at 2:26 PM, Josh Susser 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 Squires
http://somethinglearned.com
Posted by Josh Susser (jsusser)
on 02.06.2006 08:29
Trevor Squires wrote:
> On 1-Jun-06, at 2:26 PM, Josh Susser 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? :-)

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

--
Josh Susser
http://blog.hasmanythrough.com