After_save,After_create(order)(Ruby on rails)

时间:2013-04-12 03:12:39

标签: ruby ruby-on-rails-3

(Ruby on rails)

我有一个注册表单,可以将表单中的数据保存到表格和另一个表格中。我使用钩子“after_save :process_field_to_another_table”和“after_create :send_registration_email”在用户注册后向用户发送电子邮件。

不幸的是,“after_create”在“after_save”之前有效,但在保存过程发生后我需要:send_registration_email才能工作。我想在“:process_field_to_another_table”中运行after_create,但是它连续运行两次,这没有多大意义。

在这个例子中,我是如何指示“after_save”在“after_create”之前运行的?或任何更好的建议?我尝试了“before_save”和“before_create”,但它似乎没有正确处理。

任何建议?

2 个答案:

答案 0 :(得分:3)

你可以把两个钩子放在一个。

after_save :foo

def foo
  process_field_to_another_table
  send_registration_email if id_changed?  # id will change only for new record.
end

答案 1 :(得分:2)

您可以使用after_createafter_update回调的组合来分隔通话。

after_create :process_field_to_another_table, :send_registration_email
after_update :process_field_to_another_table

这样,process_field_to_another_table之前会调用send_registration_email