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.
3ccdcc71db4c8212fba584953572bf51?d=identicon&s=25 Noel R. Morais (Guest)
on 2006-06-01 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
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-06-01 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/Ba...

Put this in your model class:

def inheritance_column
  "something_other_than_type"
end

--
Josh Susser
http://blog.hasmanythrough.com
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-06-01 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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-06-02 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
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-06-02 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
This topic is locked and can not be replied to.