Hi, I am searching for a way to dynamic validate my models.
The solution that I got working is:
def after_initialize
singleton = class << self; self; end
validations = eval(calendar.cofig)
validations.each do |val|
singleton.class_eval(val)
end
end
On my actual app, I have 2 models
class Calendar < ActiveRecord::Base
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :calendar
def after_initialize
singleton = class << self; self; end
validations = eval(calendar.config)
validations.each do |val|
singleton.class_eval(val)
end
end
end
As you can see, the validation code that should be added to the Event
class lies on the Calendar field “config”.
Works fine for a existing Event, but doesn’t for a new record. That’s
because, at the time that after_initialize is called, the association
doesn’t exists yet.
I can’t find a way to do that besides putting the config values on
Event itself.
Any advices?
Tks!