Rails 接受_nested_attributes_for 回调

发布于 2024-09-01 21:29:02 字数 942 浏览 3 评论 0原文

我有两个模型 TicketTicketComment,TicketComment 是 Ticket 的子模型。

门票.rb

class Ticket < ActiveRecord::Base
  has_many :ticket_comments, :dependent => :destroy, :order => 'created_at DESC'

  # allow the ticket comments to be created from within a ticket form
  accepts_nested_attributes_for :ticket_comments, :reject_if => proc { |attributes| attributes['comment'].blank? }
end

ticket_comment.rb

class TicketComment < ActiveRecord::Base
  belongs_to :ticket

  validates_presence_of :comment
end

我想要做的是模仿 Trac 中的功能,如果用户对票证进行更改和/或添加评论,则会向分配给该票证的人员发送一封电子邮件票。

我想使用 after_update 或 after_save 回调,以便我知道在发送电子邮件之前信息已全部保存。

如何检测模型的更改 (ticket.changes) 以及是否创建了新评论 (ticket.comments) 并通过一封电子邮件发送此更新(x 更改为 y,用户添加了评论“文本”)回调方法?

I have two models Ticket and TicketComment, the TicketComment is a child of Ticket.

ticket.rb

class Ticket < ActiveRecord::Base
  has_many :ticket_comments, :dependent => :destroy, :order => 'created_at DESC'

  # allow the ticket comments to be created from within a ticket form
  accepts_nested_attributes_for :ticket_comments, :reject_if => proc { |attributes| attributes['comment'].blank? }
end

ticket_comment.rb

class TicketComment < ActiveRecord::Base
  belongs_to :ticket

  validates_presence_of :comment
end

What I want to do is mimic the functionality in Trac, where if a user makes a change to the ticket, and/or adds a comment, an email is sent to the people assigned to the ticket.

I want to use an after_update or after_save callback, so that I know the information was all saved before I send out emails.

How can I detect changes to the model (ticket.changes) as well as whether a new comment was created or not (ticket.comments) and send this update (x changes to y, user added comment 'text') in ONE email in a callback method?

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

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

发布评论

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

评论(1

烟花易冷人易散 2024-09-08 21:29:02

您可以使用 ActiveRecord::Dirty 模块,它允许您跟踪未保存的变化。

例如

t1 = Ticket.first
t1.some_attribute = some_new_value
t1.changed? => true
t1.some_attribute_changed? => true
t1.some_attribute_was => old_value 

,在 before_create 的 before_update 中你应该这些(你只能在保存之前检查!)。

收集所有这些方法的一个非常好的地方是 Observer-class TicketObserver ,这样您就可以将“观察者”代码与实际模型分开。

例如,

class TicketObserver < ActiveRecord::Observer
  def before_update
    .. do some checking here ..
  end
end

要启用观察者类,您需要将其添加到您的 environment.rb 中:

config.active_record.observers = :ticket_observer

这应该可以帮助您开始:)

涉及链接注释的内容。如果你这样做:

new_comment = ticket.ticket_comments.build
new_comment.new_record? => true
ticket.comments.changed => true

那么这正是你所需要的。这对你不起作用吗?
再次注意:当然,您需要在保存之前检查这一点:)

我想您必须收集在 before_create 或 before_update 中更改的数据,并在 after_update/create 中实际发送邮件(因为这样您就确定它成功了)。

显然目前还不清楚。我会让它更明确一点。我建议使用 TicketObserver 类。但如果你想使用回调,那就像这样:

class Ticked

  before_save :check_state
  after_save :send_mail_if_needed

  def check_state
    @logmsg=""
    if ticket_comments.changed
      # find the comment
      ticket_comments.each do |c| 
        @logmsg << "comment changed" if c.changed?
        @logmsg << "comment added" if c.new_record? 
      end
    end
  end

end
def send_mail_if_needed
  if @logmsg.size > 0
    ..send mail..
  end
end

you could use the ActiveRecord::Dirty module, which allows you to track unsaved changes.

E.g.

t1 = Ticket.first
t1.some_attribute = some_new_value
t1.changed? => true
t1.some_attribute_changed? => true
t1.some_attribute_was => old_value 

So inside a before_update of before_create you should those (you can only check before the save!).

A very nice place to gather all these methods is in a Observer-class TicketObserver, so you can seperate your "observer"-code from your actual model.

E.g.

class TicketObserver < ActiveRecord::Observer
  def before_update
    .. do some checking here ..
  end
end

to enable the observer-class, you need to add this in your environment.rb:

config.active_record.observers = :ticket_observer

This should get you started :)

What concerns the linked comments. If you do this:

new_comment = ticket.ticket_comments.build
new_comment.new_record? => true
ticket.comments.changed => true

So that would be exactly what you would need. Does that not work for you?
Note again: you need to check this before saving, of course :)

I imagine that you have to collect the data that has changed in a before_create or before_update, and in an after_update/create actually send the mail (because then you are sure it succeeded).

Apparently it still is not clear. I will make it a bit more explicit. I would recommend using the TicketObserver class. But if you want to use the callback, it would be like this:

class Ticked

  before_save :check_state
  after_save :send_mail_if_needed

  def check_state
    @logmsg=""
    if ticket_comments.changed
      # find the comment
      ticket_comments.each do |c| 
        @logmsg << "comment changed" if c.changed?
        @logmsg << "comment added" if c.new_record? 
      end
    end
  end

end
def send_mail_if_needed
  if @logmsg.size > 0
    ..send mail..
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文