2012-05-03 2 views
0

Ruby on Rails 앱을 만들고 있는데 내 모델의 일부 속성을 설정하는 데 문제가 있습니다. 문제는 내가 : before_save 메소드를 가지고 있는데, 어떤 이유로 encryption_password와 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을 사용해보세요.

액티브, 당신의 속성에 대한 즉

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

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

을 게터/세터 메소드를하게 그리고 당신은 @encrypted_password을 쓸 때 데이터베이스가 업데이트되지 않도록, 당신은 실제로 이러한 방법을 사용하지 않는 것입니다.

당신은 당신의 로그에서 볼 수 있습니다

[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는 전무로 설정하면 속성을 업데이트하지 않았기 때문에, 당신은 클래스 멤버 변수를 업데이트 중입니다.

+0

그게 효과가있다. 나는 무언가를 얻지 않는다. 이 함수가 저장 전에 실행되면 ActiveRecord의 save 메소드가 클래스 멤버 변수를 찾지 말고 저장시 데이터베이스에 저장해야합니까? –

+0

ActiveRecord는 클래스 멤버 변수를 저장하지 않습니다. 그것은 열이 멤버 변수처럼 보이는 방식으로 모델의 데이터베이스 테이블 구조를 노출합니다. 루비 문법에 대한 친숙성에 따라'foo = self.encrypted_password'는 실제로'foo = self.encrypted_password()', 즉 멤버가 아닌 메소드 호출이라는 것이 명확하지 않을 수도 있습니다. – Soup

1

난 당신이 def encrypt_password의 말에 self.encrypted_password = @encrypted_password을 잊었다 생각합니다.

+0

@encrypted_password가 self.encrypted_password와 같지 않습니까? –

관련 문제