Dynamically changing validations in Rails

Posted by user94154 on Stack Overflow See other posts from Stack Overflow or by user94154
Published on 2010-06-09T02:52:57Z Indexed on 2010/06/09 3:02 UTC
Read the original article Hit count: 211

Filed under:
|
|

I have a model with a validation. At runtime, I'd like to change a value of the validation. For example: in the model bid.rb:

class Bid ActiveRecord::Base
   @foo = Foo.find(1)
   validates_inclusion_of :amt, :in => [email protected], :message => "must be between 1 and #{@foo.bar}"
end

and in the application_controller (pseudocode):

if today == 'wednesday'
   Foo.update(1, :bar => 10)
else
   Foo.update(1, :bar => 5)
end

However, this setup isn't working. The "foo" attribute never updates. It seems that the validation code is set only when the dev server starts and then doesn't change.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby