Devise with rails 5
Asked Answered
L

2

1

I am making an app in rails 5 with devise. But after migration of user model which is used by devise the results are following:

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

while It should be shown like:

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

It means fields are not being create. but when I see the mysql database all the fields are being created inside users table. Then why it is not showing inside rails console?

Following are the 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
Licastro answered 17/7, 2016 at 7:48 Comment(1)
Did you reload! your rails console?Excipient
P
4

This is because devise overrides the inspect method, to not show their internal attributes. See this: https://github.com/plataformatec/devise/blob/29142418bade74224d98bbf5bbcadfba181d5db9/lib/devise/models/authenticatable.rb#L119-L124

Anyway, they're there. Just not printed out in the inspect method.

To see all the attributes you can use:

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

Or e.g. to get the current_sign_in_ip:

user.current_sign_in_ip # => #<IPAddr: IPv4:xxx.xxx.xxx.xxx/255.255.255.0>
Pedroza answered 22/9, 2016 at 7:44 Comment(0)
H
2

Devise restricts attributes like encrypted_password by overriding the default inspect method so that the critical information does not get exposed in API calls. So to override this, you need to override the serializable_hash method:

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

Check out the discussion here.

Hendren answered 22/9, 2016 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.