Destination with rails 5

I am making an application in rails 5 with an application. But after the migration of the user model that is used during development, the following results:

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

while It should be shown as:

 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 

This means that fields are not created. but when I see the mysql database, all fields are created inside the user table. Then why doesn't it show inside the rails console?

The following are 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
ruby-on-rails-5
source share
2 answers

This is because devise overrides the validation method so that it does not show its internal attributes. See this: https://github.com/plataformatec/devise/blob/29142418bade74224d98bbf5bbcadfba181d5db9/lib/devise/models/authenticatable.rb#L119-L124

Anyway, they are there. It just doesn't print in the inspect method.

To view all the attributes you can use:

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

Or, for example, to get current_sign_in_ip :

 user.current_sign_in_ip # => #<IPAddr: IPv4:xxx.xxx.xxx.xxx/255.255.255.0> 
+2
source share

Devise restricts attributes such as encrypted_password by overriding the default inspect method 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 

Check out the discussion here .

0
source share

All Articles