自定义验证:on =>:创建无效

时间:2021-10-01 07:13:01

I have a custom validation method that I only want executed on create:

我有一个自定义验证方法,我只想在create上执行:

validate :post_count, :on => :create

def post_count
  # validate stuff
end  

However, it's getting fired on update (in addition to on create).

然而,它在更新时被解雇(除了在创建时)。

Does the :on => :create option not work with custom validation methods?

:on =>:create选项不适用于自定义验证方法吗?

2 个解决方案

#1


9  

As far as I know, there's no :on option. Use

据我所知,没有:on选项。使用

validate_on_create :post_count

instead. And there's validate_on_update also. You can read about this methods here.

代替。而且还有validate_on_update。你可以在这里阅读这些方法。

#2


7  

This may be a Rails 2.x vs. Rails 3 issue but according to the Rails Guides on Validation the :on option is definitely valid (though I am fighting with why it's not firing for me in a similar way).

这可能是Rails 2.x vs. Rails 3问题,但根据Rails指南验证:on选项绝对有效(尽管我正在为什么它不以类似的方式为我解雇)。

#1


9  

As far as I know, there's no :on option. Use

据我所知,没有:on选项。使用

validate_on_create :post_count

instead. And there's validate_on_update also. You can read about this methods here.

代替。而且还有validate_on_update。你可以在这里阅读这些方法。

#2


7  

This may be a Rails 2.x vs. Rails 3 issue but according to the Rails Guides on Validation the :on option is definitely valid (though I am fighting with why it's not firing for me in a similar way).

这可能是Rails 2.x vs. Rails 3问题,但根据Rails指南验证:on选项绝对有效(尽管我正在为什么它不以类似的方式为我解雇)。