在rake任务期间关闭观察者的简单方法? [英] Simple way of turning off observers during rake task?

查看:101
本文介绍了在rake任务期间关闭观察者的简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用restful_authentication.我正在使用rake任务创建一组默认用户,但是每次运行该任务时,都会由于与用户模型相关联的观察者而发出激活电子邮件.我在创建用户时会设置激活字段,因此无需激活.

I'm using restful_authentication in my app. I'm creating a set of default users using a rake task, but every time I run the task an activation email is sent out because of the observer associated with my user model. I'm setting the activation fields when I create the users, so no activation is necessary.

任何人都知道在执行rake任务时绕过观察者的一种简单方法,这样在保存用户时不会发送电子邮件吗?

Anyone know of an easy way to bypass observers while running a rake task so that no emails get sent out when I save the user?

谢谢.

推荐答案

您可以向用户模型添加访问器,例如"skip_activation",它不需要保存,但可以在会话中持久保存,然后检查观察者中的标志.像

You could add an accessor to your user model, something like "skip_activation" that wouldn't need to be saved, but would persist through the session, and then check the flag in the observer. Something like

class User
  attr_accessor :skip_activation
  #whatever
end

然后,在观察者中:

def after_save(user)
  return if user.skip_activation
  #rest of stuff to send email
end

这篇关于在rake任务期间关闭观察者的简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆