2016-07-17 8 views
0

私はデマンドでレール5にアプリを作っています。しかし、工夫によって使用されているユーザモデルの移行後の結果は次のとおりです。レール付きデベロップ5

User.new => 
    User id: nil, 
    email: "", 
    created_at: nil, 
    updated_at: nil. 

それはのように示すべきである一方で:

User id: nil, 
email: "", 
encrypted_password: "", 
reset_password_token: nil, 
reset_password_sent_at: nil, 
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, 
created_at: nil, 
updated_at: nil, 
name: nil 

それはフィールドが作成されていないことを意味します。しかし、私はmysqlデータベースを参照してすべてのフィールドは、ユーザーのテーブル内で作成されている。それではなぜレールのコンソールに表示されないのですか?

後はschema.rbです:

ActiveRecord::Schema.define(version: 20160717050914) do 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.string "confirmation_token" 
    t.datetime "confirmed_at" 
    t.datetime "confirmation_sent_at" 
    t.string "unconfirmed_email" 
    t.integer "failed_attempts",  default: 0, null: false 
    t.string "unlock_token" 
    t.datetime "locked_at" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true 
    t.index ["email"], name: "index_users_on_email", unique: true 
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 
    t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true 
    end 

end 
+0

レールコンソールをリロードしましたか? –

+0

はい@zaphをやった –

答えて

2

deviseは、内部の属性を表示しないように、メソッドを検査よりも優先されますので、これはです。これを見てください:https://github.com/plataformatec/devise/blob/29142418bade74224d98bbf5bbcadfba181d5db9/lib/devise/models/authenticatable.rb#L119-L124

とにかく、そこにいます。 inspectメソッドでは印刷されません。

user = User.first 
user.attributes # => returns a hash containing all the attributes 

または例えば:あなたが使用できるすべての属性を表示するには

user.current_sign_in_ip # => #<IPAddr: IPv4:xxx.xxx.xxx.xxx/255.255.255.0> 
0

工夫の重要な情報がAPI呼び出しにさらされないようにデフォルトinspectメソッドをオーバーライドしてencrypted_passwordなどの属性を制限:current_sign_in_ipを取得します。だから、これを無効にするために、あなたはserializable_hashメソッドオーバーライドする必要があります:

def serializable_hash(options = nil) 
    super(options).merge({ encrypted_password: encrypted_password }) 
end 

を議論hereをチェックしてください。

関連する問題