2012-05-03 8 views
0

私はRuby on Railsアプリケーションを作成していますが、私のモデルのいくつかの属性を設定する際に問題があります。問題は、私は:before_saveメソッドを持っていますが、何らかの理由でencrypted_pa​​sswordとsaltがデータベースに保存されていないことです。こちらのモデルです:Ruby on Railsモデルの属性がbefore_saveで設定されていないのはなぜですか?

Started POST "/users" for 127.0.0.1 at Wed May 02 22:55:20 -0500 2012 
    Processing by UsersController#create as HTML 
     Parameters: {"commit"=>"Submit", "authenticity_token"=>"RY9fSMqb2+tdQ0fIjiEz8cfMTWTi012vCWdCvbxACLk=", "utf8"=>"\342\234\223", "user"=>{"username"=>"test6", "password"=>"[FILTERED]", "email"=>"[email protected]"}} 
     [1m[36m (0.1ms)[0m [1mbegin transaction[0m 
     [1m[35mUser Exists (0.2ms)[0m SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    Got to encrypt_password 1 
    encrypted_password is 
    Got to encrypt_password 2 
    New salt = 4f3464029393829aa562e533773f668c8471c51231611f6f214e654275f37184 
    New encrypted_password = 0dafcff2fe75bb6f2b53afda79789cfe13bd3f733b817a0e2e30df98af5829bc 
    Got to encrypt_password 3 
     [1m[36mSQL (0.5ms)[0m [1mINSERT INTO "users" ("created_at", "email", "encrypted_password", "is_done", "last_seen", "salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m [["created_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", nil], ["is_done", false], ["last_seen", nil], ["salt", nil], ["updated_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["username", "test6"]] 
     [1m[35m (0.7ms)[0m commit transaction 
    Redirected to http://localhost:3000/ 
    Completed 302 Found in 7ms (ActiveRecord: 1.4ms) 

だから、それは間違いなく、塩と暗号化されたパスワードを作っている:ログ・ファイルで

class User < ActiveRecord::Base 
    attr_accessor :password 
    attr_accessible :username, :email, :password 

    before_save :encrypt_password 

    ... 

    private 
     def encrypt_password 
     logger.debug "Got to encrypt_password 1" 
     logger.debug "encrypted_password is #{ @encrypted_password }" 

     if @encrypted_password != nil and @encrypted_password != "" 
      return # @encrypted_password was already set 
     end 

     logger.debug "Got to encrypt_password 2" 
     @salt = make_salt if new_record? 
     logger.debug "New salt = #{ @salt }" 
     @encrypted_password = encrypt(@password) 
     logger.debug "New encrypted_password = #{ @encrypted_password }" 
     @password = "(encrypted)" 
     logger.debug "Got to encrypt_password 3" 
     end 

、私は以下を参照してください。しかし、データベースは更新されていませんか?

>> User.find(6) 
=> #<User id: 6, username: "test6", email: "[email protected]", encrypted_password: nil, is_done: false, salt: nil, last_seen: nil, created_at: "2012-05-03 03:55:20", updated_at: "2012-05-03 03:55:20"> 
+0

現在使用しているレールのバージョンは? – Suborx

+0

@Suborx - Rails 3.2.3 –

答えて

2

self.encrypted_passwordをお試しください。

ActiveRecordのは

def encrypted_password 
    ... 
    # return something from the db 
end 

def encrypted_password=(x) 
    ... 
    # set something in the db to x 
end 

そして、あなたは@encrypted_passwordを書くとき、あなたが実際にこれらのメソッドを使用していないので、データベースが更新されません。つまり、あなたの属性のgetter/setterメソッドを作成します。

あなたのログにこれを見ることができます:あなたは属性を更新していなかったので、

[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "users" ("created_at", "email", "encrypted_password", "is_done", "last_seen", "salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m [["created_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", nil], ["is_done", false], ["last_seen", nil], ["salt", nil], ["updated_at", Thu, 03 May 2012 03:55:20 UTC +00:00], ["username", "test6"]]

saltencrpyed_passwordは、nilに設定されている、あなたはクラスのメンバ変数を更新しました。

+0

それはうまくいった。私は何かを得ることはありません。この関数を保存する前に実行すると、ActiveRecordのsaveメソッドがクラスメンバー変数を見つけて、保存時にデータベースに保存しないでください。 –

+0

ActiveRecordはクラスメンバー変数を保存しません。これは、列がメンバー変数のように見えるように、モデルのデータベーステーブル構造を公開します。 Rubyの構文に精通しているかどうかに応じて、 'foo = self.encrypted_pa​​ssword'は実際には' foo = self.encrypted_pa​​ssword() 'であることは明らかではないかもしれません。 – Soup

1

私はあなたがdef encrypt_passwordの終わりにself.encrypted_password = @encrypted_passwordを忘れてしまったと思います。

+0

@encrypted_pa​​sswordはself.encrypted_pa​​sswordと同じではありませんか? –

関連する問題