views:

89

answers:

2

Assume, we have a model, let's call it Articles. There, we have two columns, min and max. The rule is: min can't be greater or equal to max. How do you resolve this custom type of validation? Any ideas?

Going further, let's assume that we have columns character and made_of. Task is to create a validation which is gonna help to avoid a situation, when "character = human" and "made_of = steel".

Is there are standard routines to manage "metaphysical" conflicts between columns?

A: 

There is generic method validate which can take care of your all validations between columns. Be careful though, looks like your needs for validations require lots of testing.

Eimantas
+2  A: 

You could use validate to define your own validations. For instance:

class Article < ActiveRecord::Base
  validate :min_greater_than_max, :humans_aint_made_of_steel

  def min_greater_than_max
    errors.add(:min, "can't be greater than max") if min > max
  end 

  def humans_aint_made_of_steel
    errors.add(:human, "can't be made of steel") if character == 'human' && made_of == 'steel'
  end

end
hgimenez
Thank you, hgimenez!
gmile
You bet @gmile, although I am made of steel, and I happen to be human as well ;)
hgimenez