开发者

Validate model for certain action

I need to validate a model only for a certain action (:create). I know this is not a 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 cannot have my custom check_gold validation execute on edit, but only on create (since checking gold has to be done, only when alliance is created, not edited).

Thanx for reading :)


I'm appending some actual code :

  attr_accessor开发者_JS百科 :required_gold, :has_alliance
  validate :check_gold
  validate :check_has_alliance

This is the Alliance model. :required_gold and :has_alliance are both set in the controller(they are virtual attributes, because i need info from the controller). Now, the actual validators are:

  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

This works great for create, but i want to restrict it to create alone and not edit or the other actions of the scaffold.


All ActiveRecord validators have a :on option.

validates_numericality_of :value, :on => :create 

Use the validate_on_create callback instead of validate:

validate_on_create :check_gold
validate_on_create :check_has_alliance

Edit:

If you use validates_each you can use the standard options available for a validator declaration.

validates_each :required_gold, :has_alliance, :on => :create do |r, attr, value|
  r.check_gold if attr == :required_gold
  r.check_has_alliance if attr == :has_alliance
end


Like Sam said, you need a before_create callback. Callbacks basically mean 'execute this method whenever this action is triggered'. (More about callbacks here : http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html).

This is what you want in your model:

before_create :check_gold

# other methods go here

private # validations don't need to be called outside the model

def check_gold
  # do your validation magic here
end

The above method is the simplest to do what you want, but FYI there's also a way to use a before_save callback to execute additional actions on creation:

before_save :check_gold_levels

# other methods

private
def check_gold_levels
  initialize_gold_level if new? # this will be done only on creation (i.e. if this model's instance hasn't been persisted in the database yet)
  verify_gold_level # this happens on every save
end

For more info on 'new?' see http://api.rubyonrails.org/classes/ActiveResource/Base.html#method-i-new%3F


You need to look into callbacks. Someone once told me this and I didn't understand what they meant. Just do a search for rails callbacks and you will get the picture.

In your model you need to do a callback. The callback you need is before_create and then before a object is created you will be able to do some logic for check for errors.

model.rb

before_create :check_gold_validation

def check_gold_validation
    validate :check_gold
end 

def check_gold
   errors.add_to_base "Some Error" if self.some_condition?
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜