0
我试图做一个validates_length_of,但在运行时指定范围/最小值/最大值。dynamic validates_length_of
例如,我们有一个父模型:
class Parent < ActiveRecord::Base
has_many :children
# with attributes min_length, max_length
end
和子模型:
class Child < ActiveRecord::Base
belongs_to :parent
# with an attribute reference
end
所以我想在子类中做的是:
validate :reference_length
def reference_length
options = { :within => parent.min_length..parent.max_length }
self.class.validates_length_of :reference, options
end
但它不起作用,有没有办法做到这一点,而不是做errors.add(:reference, message) if...
?
好,基本上你只能指定MIN_LENGTH(而不是MAX_LENGTH),所以你不应该验证最大长度,反之亦然... – jordinl 2010-10-01 15:08:17