Jaime Cham
2007-Jul-06 04:37 UTC
validations of "type" when using Single Table Inheritance
Sorry if I''m just dumbing out, but is there a reason that when using Single Table Inheritance Rails doesn''t really validate the "type" field or provide some mechanism to do so? seems like a good way to corrupt a database. Because the "type" is also a special attribute, I had to resort to an ugly hack like: class ActiveRecord::Base def type_attr self[:type] end def self.validates_type(options={}) validates_inclusion_of :type_attr, options end end so that I could write: class Fruit < ActiveRecord::Base validates_type :allow_nil => true, :in => %w{ Apple } # could be dynamic subclass_of but make faster! (obviously this could also have been put on the derived class) Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy
2007-Jul-08 22:07 UTC
Re: validations of "type" when using Single Table Inheritanc
I''d dare to say the reason "type" can''t be validated is that it inherently has to exist. After all, the "type" is exactly the name of the class on which you''re calling "create" if the "type" doesn''t exist, then you can''t be trying to save it. Jaime Cham wrote:> Sorry if I''m just dumbing out, but is there a reason that when using > Single Table Inheritance Rails doesn''t really validate the "type" > field or provide some mechanism to do so? seems like a good way to > corrupt a database. Because the "type" is also a special attribute, I > had to resort to an ugly hack like: > > class ActiveRecord::Base > def type_attr > self[:type] > end > def self.validates_type(options={}) > validates_inclusion_of :type_attr, options > end > end > > so that I could write: > > class Fruit < ActiveRecord::Base > > validates_type :allow_nil => true, > :in => %w{ Apple } # could be dynamic > subclass_of but make faster! > > (obviously this could also have been put on the derived class) > > Thanks!-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jaime Cham
2007-Jul-10 08:03 UTC
Re: validations of "type" when using Single Table Inheritanc
Yes, although it could be changed manually (unlikely) or injected from cgi parameters, which is what the validations are supposed to prevent, no? On Jul 9, 6:07 am, Matthew Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''d dare to say the reason "type" can''t be validated is that it > inherently has to exist. > > After all, the "type" is exactly the name of the class on which you''re > calling "create" > > if the "type" doesn''t exist, > then you can''t be trying to save it. > > > > JaimeCham wrote: > > Sorry if I''m just dumbing out, but is there a reason that when using > > Single Table Inheritance Rails doesn''t really validate the "type" > > field or provide some mechanism to do so? seems like a good way to > > corrupt a database. Because the "type" is also a special attribute, I > > had to resort to an ugly hack like: > > > class ActiveRecord::Base > > def type_attr > > self[:type] > > end > > def self.validates_type(options={}) > > validates_inclusion_of :type_attr, options > > end > > end > > > so that I could write: > > > class Fruit < ActiveRecord::Base > > > validates_type :allow_nil => true, > > :in => %w{ Apple } # could be dynamic > > subclass_of but make faster! > > > (obviously this could also have been put on the derived class) > > > Thanks! > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---