Rails将参数/参数传递给ActiveRecord回调函数

编程入门 行业动态 更新时间:2024-10-27 04:28:48
本文介绍了Rails将参数/参数传递给ActiveRecord回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的一个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回调函数

本文发布于:2023-10-28 13:15:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   回调   函数   Rails   ActiveRecord

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!