ActiveRecord如何实现`:if =>; ...` 关于验证
我一直在浏览 ActiveRecord 源代码以了解 :if => 是如何实现的。 proc_or_method_name
适用于 ActiveRecord 验证,但源代码中 :if
的唯一实例位于解释如何调用该功能的注释中。
例如,您可以在模型中包含如下行:
validates_presence_of :name, :if => :nameable?
并且仅当 nameable?
方法返回给定模型的真值时才会检查验证。
这个功能实际上在哪里定义的,因为我在(Rails2)源代码中的任何地方都找不到这个行为?
I've been looking through the ActiveRecord source to find out how :if => proc_or_method_name
works on ActiveRecord validations, but the only instances of :if
in the source are in the comments explaining how the feature should be called.
For example, you can have a line like the following in a model:
validates_presence_of :name, :if => :nameable?
and the validation only gets checked if the nameable?
method returns a truthy value for the given model.
Where is this functionality actually defined, as I can't find this behaviour anywhere in the (Rails2) source?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在文件
activesupport\lib\active_support\callbacks.rb
中检查:if
选项。调用方法
should_run_callback
来检查是否应该执行回调。另请参阅如何处理回调链,从同一文件中的
run_callbacks
方法开始。该文件 v2.3.8 中的一些代码是:
以下是我发现的方法(如果有人感兴趣):
:if
进行 grep 编辑activerecord/CHANGELOG
中,找到一条注释,其中提到:向所有验证添加了 :if 选项,这些验证可以使用块或方法指针来确定是否应运行验证。 #1324 [杜安·约翰逊/jhosteny]。
:if
的工作原理:if
并浏览每个感觉“良好”的文件。来到activesupport/lib/active_support/callbacks.rb
:if
,只在一个位置找到了它,在方法should_run_callback.
很有趣!
The
:if
option is checked for in the fileactivesupport\lib\active_support\callbacks.rb
.The method
should_run_callback
is called to check if the callback should be executed or not.Look also at how the callback chain is processed, starting from the
run_callbacks
method in the same file.Some code from v2.3.8 of that file is:
And here is how I found out (in case anyone is interested):
:if
in all .rb filesactiverecord/CHANGELOG
, located a comment that mentioned:Added the :if option to all validations that can either use a block or a method pointer to determine whether the validation should be run or not. #1324 [Duane Johnson/jhosteny].
:if
works:if
again and went though each file that felt "good". came toactivesupport/lib/active_support/callbacks.rb
:if
in the file and it was found in only one location, in the methodshould_run_callback
.That was fun!
从 Rails 3 开始,ActiveRecord 回调在 active_record 中定义/callbacks.rb,但是因为 ActiveRecord 模型继承自 ActiveModel,所以您还应该查看 active_model/callbacks.rb 文件。
回调功能本身是一个单独的组件。事实上,ActionController 之前/之后的过滤器实际上是回调。
因此,回调系统是 ActiveSupport::回调。
将这 3 部分合并在一起,您将获得 ActiveRecord 回调功能。
As of Rails 3, ActiveRecord callbacks are defined in active_record/callbacks.rb, but because an ActiveRecord model inherits from ActiveModel, then you should also look at the active_model/callbacks.rb file.
The Callback feature itself is a separate component. In fact, ActionController before/after filters are callbacks, actually.
For this reason, the callback system is a module defined in ActiveSupport::Callbacks.
Merge altogether these 3 pieces and you get the ActiveRecord callbacks feature.