2013-09-25 20 views
0

對,這是我遇到過的最奇怪的錯誤。什麼會導致對象的屬性不能從數據庫中讀取?

我在資源中有一個名爲hashed_pa​​ssword的數據類型字段的字段。沒有東西可以從中讀取。這是瘋了。

用戶表

id | username | created_at | updated_at | hashed_password 

例如數據

37 | john harris | 2013-09-24 | 2013-09-24 | eba082ff45517c06b 

用戶索引

<% @users.each do |user| %> 
    <tr> 
    <td><%= user.id %></td> 
    <td><%= user.username %></td> 
    <td><%= user.password.inspect %></td> 
    <td><%= user.hashed_password.inspect %></td> 
    <td><%= user.created_at %></td> 
    <td><%= user.updated_at %></td> 
    <td><%= link_to 'Show', user %></td> 
    <td><%= link_to 'Edit', edit_user_path(user) %></td> 
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
<% end %> 

HTML

約翰·哈里斯

2013年9月24日

2013年9月24日

它坐在那裏在數據庫中,但它是絕對的t不能被讀取。這太奇怪了。它只存在爲零。

關於它和其他列的唯一不同是它的編程設置。這裏的用戶控制器:

require "digest/sha1" 
class User < ActiveRecord::Base 
    attr_accessor :password 

    before_create :hashed_password 

    def self.authenticate(username, password) 
     logger.info "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" 
     logger.info "user " + username 

     user = find_by_username(username) 

     logger.info "user id: " + user.id.to_s 
     logger.info "user username: " + user.username.to_s 
     logger.info "user hashed_password: " + user.hashed_password.to_s 
     logger.info "user created_at: " + user.created_at.to_s 
     logger.info "user updated_at: " + user.updated_at.to_s 
     logger.info "*-*-*-*-*-*-*-*-" 
     logger.info "entered hashed password " + Digest::SHA1.hexdigest(password) 

     if user && user.hashed_password == Digest::SHA1.hexdigest(password) 
      logger.info "SUCCESS" 
      user 
     else 
      nil 
     end 
    end 

    def hashed_password 
     if password.present? 
      self.hashed_password = Digest::SHA1.hexdigest(password) 
     end 
    end 

但是,一旦它在不應該有所作爲的數據庫,應該嗎?它只是拒絕閱讀。此外,該日誌文件:

Started POST "/sessions" for 127.0.0.1 at 2013-09-25 08:06:45 +0100 
Processing by SessionsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"p5tDog8wOD6+H3fSFZpDXkMxNzFxej3tay6slVmIF5Y=", "username"=>"john harris", "password"=>"[FILTERED]", "commit"=>"Log in"} 
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
user john harris 
    [1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."username" = 'john harris' LIMIT 1 
user id: 37 
user username: john harris 
user hashed_password: 
user created_at: 2013-09-25 07:06:28 UTC 
user updated_at: 2013-09-25 07:06:28 UTC 
*-*-*-*-*-*-*-*- 
entered hashed password eba082ff45517c06bd365c2fde1fc77cda7a8f6f 
    Rendered sessions/new.html.erb within layouts/application (1.0ms) 
Completed 200 OK in 11ms (Views: 8.0ms | ActiveRecord: 0.0ms) 

用戶hashed_pa​​ssword的空白:條目。到底是怎麼回事?一旦數據在數據庫中,無論它到達那裏,它都應該能夠被讀取。太奇怪了。

我唯一能想到的是hashed_pa​​ssword是一個關鍵字,rails對它做了一些愚蠢的事情。這就是我能想到的。完全無所適從。

+0

請在schema.rb –

回答

0

用於設置hashed_pa​​ssword的回調方法具有與屬性相同的名稱,因此將其重命名爲唯一的內容,因爲它會覆蓋屬性訪問器。

+0

中發佈與用戶有關的方法再次您好。非常感謝...真正愚蠢的錯誤。好吧,現在一切正常。那麼簡單地在模型中創建一個方法就會創建一個屬性?有趣... – Starkers

+0

不,通過訪問者的方法訪問屬性,參見[this](http://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby)來獲得這個想法。對於activerecord屬性,自動創建類似的訪問器。當你命名一些與屬性相同的方法時,你可以覆蓋它的默認讀取存取器。 – biomancer

相關問題