I need to check the model only for a specific action (: create). I know this is not a very good tactic, but I just need to do this in my case.
I tried using something like:
validate :check_gold, :if => :create
or
validate :check_gold, :on => :create
But I get errors. The problem is that I can’t check the user checks check_gold when editing, but only to create (since the gold check should only be done when creating the alliance, not editing).
Thanx for reading :)
I am adding some actual code:
attr_accessor :required_gold, :has_alliance
validate :check_gold
validate :check_has_alliance
This is the Alliance model .: required_gold and: has_alliance are set in the controller (they are virtual attributes because I need information from the controller). Valid validators are now:
def check_gold
self.errors.add(:you_need, "100 gold to create your alliance!") if required_gold < GOLD_NEEDED_TO_CREATE_ALLIANCE
end
def check_has_alliance
self.errors.add(:you_already, "have an alliance and you cannot create another one !") if has_alliance == true
end
, , .