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