Rails 5 model objects not showing all Devise attributes
Asked Answered
C

3

8

I am using Rails 5 API with devise. I have a User model. Schema looks like this.

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.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token",    unique: true, using: :btree
  end

But the issue I am having is Rails only showing :id , :email , :created_at , :updated_at attributes as part of model. For example, in rails console

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

 User.first
  User Load (0.5ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => #<User id: 2, email: "[email protected]", created_at: "2016-09-22 03:58:04", updated_at: "2016-09-22 04:54:41"> 

But these attributes exist in database.This problem is mentioned earlier . Devise with rails 5 . But no answers there .Please help.

Calve answered 22/9, 2016 at 7:21 Comment(1)
User.first.to_jsonSpectral
P
12

Devise restricts attributes like encrypted_password so that the critical information doesn't get exposed in API calls. So to override this, you need to override serializable_hash method.

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

This is not a Rails 5 specific feature but a Devise feature to protect your attributes.

Hope that helps!

Palate answered 22/9, 2016 at 7:53 Comment(4)
Yes. Thats what I was looking for. This didnt happen in earlier Rails 4.2 projects built with devise. Not sure why ?Calve
I am not sure but Devise used to do this in Rails 3, but this is interesting. I will look into it.Palate
I just checked in some of my earlier Rails 4 projects , all attributes are shown there. Devise 3.5.5.Calve
Why not just use .attributes instead of overriding devise methods?Squilgee
S
10

This is probably because devise does not expose their internal attributes.

So to get all attributes you can use .attributes (documented here) which returns a hash, on which you can call to_json:

user = User.find(1)
user.attributes.to_json # => contains all fields like reset_password_token etc.
Squilgee answered 22/9, 2016 at 7:39 Comment(0)
C
5

Try attributes method

User.first.attributes
Chatty answered 22/9, 2016 at 7:24 Comment(4)
That gives all the attributes. But why only some attributes are appearing for User.first . For example , @user.to_json doesnt add all attributes. Thus messing up devise gem's json responses.Calve
try @user.attributes.to_jsonMitchell
Hey Bartek, I can get the attributes this way. But my question is , why doesnt User.first show all attributes. ?Calve
I think devise protect against something data lake.Mitchell

© 2022 - 2024 — McMap. All rights reserved.