You can set errors , but do so as part of a validation method, for example:
validate :must_rhyme_with_orange def must_rhyme_with_orange unless rhymes_with_orange? errors.add(:name, "doesn't rhyme with orange") end end
If you want to conditionally run a check, one trick is to use attr_accessor and the protection state:
attr_accessor :needs_rhyming validate :must_rhyme_with_orange, :if => Proc.new {|o| o.needs_rhyming} > u = User.last > u.needs_rhyming = true > u.valid?
source share