Rails after_create created_at未在创建的对象上设置

时间:2017-06-19 15:17:52

标签: ruby-on-rails

我在我的Message模型上有一个after_create,它创建一个像这样的Notification的新实例。

after_create :send_notification

def send_notification
  n = Notification.new :name => "#{self.sender.smart_name} sent you a message:", :user_id => self.receiver_id, :notification_type => 'message', :subject => self.subject
  n.save
end

但是,创建的对象都将created_at和updated_at设置为nil。

#<Notification:0x0000000c486208
  id: 123123,
  user_id: 3423,
  name: "I sent you a message:\n" + "10:27",
  notification_type: "message",
  created_at: nil,
  updated_at: nil>

我已经检查过根据this answer将model.record_timestamps设置为true。

我没有按照建议here在active_record上设置任何内容。

我在Rails 4上使用Mysql。

1 个答案:

答案 0 :(得分:0)

你应该在n.save之后调用n.reload,以便在保存

之后读取时间戳
相关问题