2014-07-04 50 views
0

我遇到問題。我試圖使用控制檯保存用戶,但它不起作用,我不知道是什麼問題。ActiveRecord不保存有效屬性

這裏是日誌:

2.0.0-p353 :095 > u = User.find_by_email('[email protected]') 
=> #<User id: 5359, created_at: "2012-01-14 12:39:01", updated_at: "2014-07-04 09:48:06", email: "[email protected]", encrypted_password: "xxxx.", role_id: nil, reset_password_token: "xxxx...", reset_password_sent_at: "2014-07-02 15:21:06", remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmed_at: "2014-07-04 09:46:08", confirmation_sent_at: nil, confirmation_token: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, legacy_pass: "xxxxx", username: "Mr. Brightside", discourse_id: nil, my_draft_comments_count: 0, my_published_comments_count: 37, my_comments_count: 37, draft_comcoms_count: 0, published_comcoms_count: 0, deleted_comcoms_count: 0, spam_comcoms_count: 0, mailchimped: false, slug: "mr-brightside", accept_rules: true, accept_privacy: true> 
2.0.0-p353 :096 > u.username = 'brightside' 
=> "brightside" 
2.0.0-p353 :097 > u.valid? 
=> true 
2.0.0-p353 :098 > u.save! 
=> true 
2.0.0-p353 :099 > u 
=> #<User id: 5359, created_at: "2012-01-14 12:39:01", updated_at: "2014-07-04 09:48:06", email: "[email protected]", encrypted_password: "$2a$10$CdvtY4/TnwTpzDKJdFv/HOoQmIemvWfltrrqluWH.8qf...", role_id: nil, reset_password_token: "be7b04448caaf9a59a1204a6d2605b0f7257d9a8c8d5d08fa73...", reset_password_sent_at: "2014-07-02 15:21:06", remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, confirmed_at: "2014-07-04 09:46:08", confirmation_sent_at: nil, confirmation_token: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, legacy_pass: "bf59677e3dfb10370293efb5568f1a37", username: "Mr. Brightside", discourse_id: nil, my_draft_comments_count: 0, my_published_comments_count: 37, my_comments_count: 37, draft_comcoms_count: 0, published_comcoms_count: 0, deleted_comcoms_count: 0, spam_comcoms_count: 0, mailchimped: false, slug: "mr-brightside", accept_rules: true, accept_privacy: true> 
2.0.0-p353 :100 > 

任何想法?

+0

您是否看到在控制檯中執行正確的sql查詢? – SunnyMagadan

+0

@SunnyMagadan我正在生產......沒有sql查詢 –

+0

保存後用戶的電子郵件爲什麼/如何不同?它從'xxxx @ gmail.com'到'andysal @ gmail.com'。在保存代碼之前是否有任何回調? – tirdadc

回答

0

按照documentationsave!

屬性標記爲只讀如果記錄被更新被忽略。

因此檢查是否是這種情況。否則它應該按預期工作。

另外,要檢查數據庫中實際保存的記錄,請在控制檯中調用u.reload。您在最後一行檢查的對象與原始對象具有相同的updated_at時間戳,因此它或者根本沒有更新,或者沒有從數據庫重新加載(除非您鍵入的速度很快)。