gpt4 book ai didi

ruby-on-rails - 如何为所有模型创建一个通用的 after_create 回调?

转载 作者:行者123 更新时间:2023-12-01 13:32:08 25 4
gpt4 key购买 nike

假设我有用户、帐户和产品模型。我想要所有这些模型的通用 after_create 回调,比如 log_creation。

我该怎么做?

最佳答案

您可以拥有一个GenericModule 并将其包含在您希望的任何模型中

module GenericModule
extend ActiveSupport::Concern

included do
after_create :log_creation
end

def log_creation
# perform logging
end
end

在模型中

class User < ActiveRecord::Base
include GenericModule

# ...other model code...
end

您可以为所有需要这种行为的模型使用它。

关于ruby-on-rails - 如何为所有模型创建一个通用的 after_create 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45445744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com