2017-06-19 64 views
0

我在我的消息模型中创建了一个after_create,它创建了一个像这样的Notification的新实例。Rails after_create created_at未在创建的对象上设置

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> 

我检查地看到,model.record_timestamps就是真正的基于this answer设置。

我没有任何关于active_record的设置,建议here

我使用MySQL on Rails的4

+0

看起来他们不节约的。 '.save'默默地失败 - 尝试使用'.save!',这将会大声地失败,并且如果有的话会给你一个错误。 – octopushugs

+0

是记录在数据库中的条目。当您使用值初始化模型时,我还会在代码中看到一个拼写错误。 – user3775217

+0

该条目被记录在数据库中。我试过保存!事先也是如此,而且看起来很好。 –

回答

0

你应该n.save后打电话n.reload只是为了让阅读后的时间戳保存