Noel R. Morais
2006-Jun-01 20:54 UTC
[Rails] 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
Josh Susser
2006-Jun-01 21:27 UTC
[Rails] Re: What i do if i have a table with a column named "type"?
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 via http://www.ruby-forum.com/.
Bill Walton
2006-Jun-01 21:35 UTC
[Rails] What i do if i have a table with a column named "type"?
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 Squires
2006-Jun-01 23:30 UTC
[Rails] Re: What i do if i have a table with a column named "type"?
On 1-Jun-06, at 2:26 PM, Josh Susser wrote:> 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 >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
Josh Susser
2006-Jun-02 06:29 UTC
[Rails] Re: Re: What i do if i have a table with a column named "typ
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 -- Posted via http://www.ruby-forum.com/.