Rails将参数/参数传递给ActiveRecord回调函数 [英] Rails pass params/arguments to ActiveRecord callback function
问题描述
我的一个AR模型中有以下代码段:
I have the snippet below in one of my AR models:
after_update :cache_bust
模型中的 cache_bust
方法接受用于设置值的参数(布尔值)本身默认为 false
。
The cache_bust
method in the model accepts a parameter (boolean) that sets the value of itself to false
by default.
如何传递 true
到模型中从上面定义的ActiveRecord回调中的此方法?
How can I pass true
to this method in the model from my ActiveRecord callback defined above it?
例如: after_update:cache_bust(true)
因为它是一个符号而无法工作,如何将 true
传递给方法的参数?
Ex: after_update :cache_bust(true)
does not work because it's a symbol, how can I pass true
to the method's param?
推荐答案
回调宏接受四种类型的回调:方法引用(符号),回调对象,内联方法(使用proc)和内联eval方法(
There are four types of callbacks accepted by the callback macros: Method references (symbol), callback objects, inline methods (using a proc), and inline eval methods (using a string).
尝试一下吗?
after_update -> { cache_bust(true) }
这篇关于Rails将参数/参数传递给ActiveRecord回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!