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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Noel R. Morais (Guest)
on 2006-06-02 00: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
Josh S. (Guest)
on 2006-06-02 01: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/Ba...

Put this in your model class:

def inheritance_column
  "something_other_than_type"
end

--
Josh S.
http://blog.hasmanythrough.com
Bill W. (Guest)
on 2006-06-02 01: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
Trevor S. (Guest)
on 2006-06-02 03:30
(Received via mailing list)
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
Josh S. (Guest)
on 2006-06-02 10:29
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? :-)

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

--
Josh S.
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.