Rails 5 model objects that do not display all Devise attributes

I am using the Rails 5 API with development. I have a User model. The scheme is as follows.

 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 problem I ran into is Rails showing only attributes :id , :email , :created_at , :updated_at as part of the model. For example, in the 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: " your@email.com ", created_at: "2016-09-22 03:58:04", updated_at: "2016-09-22 04:54:41"> 

But these attributes exist in the database. This issue is mentioned earlier. Work out rails 5 . But there are no answers. Please, help.

+7
ruby-on-rails ruby-on-rails-5 devise
source share
3 answers

Devise restricts attributes such as encrypted_password so that critical information does not appear in API calls. To override this, you need to override the serializable_hash method.

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

This is not a special Rails 5 function, but a Devise function to protect your attributes.

Hope this helps!

+8
source share

Perhaps this is due to the fact that devise does not disclose its internal attributes.

So, to get all the attributes, you can use .attributes (registered here ), which returns a hash that you can call to_json

 user = User.find(1) user.attributes.to_json # => contains all fields like reset_password_token etc. 
+5
source share

Try the attributes method

 User.first.attributes 
+1
source share

All Articles