验证方法调用仅适用于创建,而不适用于更新

2 ruby ruby-on-rails jruby jrubyonrails ruby-on-rails-2

我在我的模型中有验证方法

def validate
  super    
  if some condition
    errors.add('', 'some text')
  end
end
Run Code Online (Sandbox Code Playgroud)

此方法调用Create和Update.我不想打电话给Update.我怎样才能做到这一点?

我正在使用rails2.3.11和jruby.

更新:我可以使用这个validate :custom_validation, :on => :create,但他们如何调用创建和更新?

我也检查了一下validate_on_create,但是当我打电话的时候我仍然没有想到validate

Sal*_*lil 7

Use

validate :custom_validation, :on => :create
Run Code Online (Sandbox Code Playgroud)

and change your method name from validate to custom_validation i.e.

def custom_validation
  super    
  if some condition
    errors.add('', 'some text')
  end
end
Run Code Online (Sandbox Code Playgroud)

and the above method will call only on create and not on update