创建钩子后设计

发布于 2024-11-19 22:40:57 字数 93 浏览 2 评论 0原文

是否有一个我可以实现的挂钩或回调,以便在创建用户后立即调用一些自定义代码?

我在用户模型中尝试了 after_confirmation 挂钩,但这不起作用。

Is there a hook or callback that I can implement so that right after the user is created, I would like to invoke some custom code ?

I tried after_confirmation hook in the user model but that didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-11-26 22:40:57

使用 Rails 提供的标准 after_create 回调。

class User < ActiveRecord::Base
  after_create :do_something

  def do_something
    puts "Doing something"
  end
end

Use the standard after_create callback provided by Rails.

class User < ActiveRecord::Base
  after_create :do_something

  def do_something
    puts "Doing something"
  end
end
始于初秋 2024-11-26 22:40:57

如果您正在处理您创建的模型的内部状态,那么使用回调是完全合法的。

创建用户后,我需要创建默认的团队。最好避免使用回调来处理其他对象

“after_*”回调主要用于保存或持久化对象。一旦对象被保存,该对象的目的(即职责)就已经实现了,所以我们通常看到的是回调到达其职责范围之外,这就是我们遇到问题的时候。

摘自这篇精彩的博客文章

在这种情况下,最好在控制器上执行操作,您可以在其中直接添加功能,或者委托给服务以获得更干净的解决方案:

# shell
rails g devise:controllers users

# config/routes.rb
devise_for :users, controllers: { registrations: "users/registrations" }

# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
  after_action :create_default_team, only: :create

  private

  def create_default_team
    Team.create_default(@user) if @user.persisted?
  end
end

Using a callback is perfectly legit if you're dealing with the internal state of the model you created.

After creating a User, I needed to create default a Team. It's preferable to avoid using callbacks to deal with other objects.

“after_*” callbacks are primarily used in relation to saving or persisting the object. Once the object is saved, the purpose (i.e. responsibility) of the object has been fulfilled, and so what we usually see are callbacks reaching outside of its area of responsibility, and that’s when we run into problems.

From this awesome blog post.

In this case it's better to act on the controller, where you can add your functionality directly, or delegate to a service for an even cleaner solution:

# shell
rails g devise:controllers users

# config/routes.rb
devise_for :users, controllers: { registrations: "users/registrations" }

# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
  after_action :create_default_team, only: :create

  private

  def create_default_team
    Team.create_default(@user) if @user.persisted?
  end
end
沫离伤花 2024-11-26 22:40:57

我正在使用带有 confirmable 的 Rails 4 和 Devise 3.5,由于各种意外,我不得不这样做。

class User < ActiveRecord::Base
  # don't use after_create, see https://github.com/plataformatec/devise/issues/2615
  after_commit :do_something, on: :create

  private

    def do_something
      # don't do self.save, see http://stackoverflow.com/questions/22567358/
      self.update_column(:my_column, "foo")
    end
end

I'm using Rails 4 with Devise 3.5 with confirmable and had to do this due to various surprises.

class User < ActiveRecord::Base
  # don't use after_create, see https://github.com/plataformatec/devise/issues/2615
  after_commit :do_something, on: :create

  private

    def do_something
      # don't do self.save, see http://stackoverflow.com/questions/22567358/
      self.update_column(:my_column, "foo")
    end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文