我有问题。我试图使用控制台保存用户,但它无法正常工作,我无法确定问题所在。
这是日志:
2.0.0-p353 :095 > u = User.find_by_email('xxxx@gmail.com')
=> #<User id: 5359, created_at: "2012-01-14 12:39:01", updated_at: "2014-07-04 09:48:06", email: "xxxx@gmail.com", 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: "xxxx@gmail.com", 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 :(得分:0)
根据save!
的{{3}}:
如果正在更新记录,则会以静默方式忽略标记为只读的属性。
所以检查是否是这种情况。否则它应该按预期工作。
此外,要从数据库中检查实际保存的记录,您可能需要在控制台中调用u.reload
。您在最后一行检查的对象与原始时间戳具有相同的updated_at
时间戳,因此要么根本没有更新,要么没有从DB重新加载(除非您快速键入reaaaaally)。