2017-06-19 62 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只是爲了讓閱讀後的時間戳保存