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!
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!
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 R. [email protected]