问题
I created a devise model
migration is:
t.string :provider, null: false, default: "email"
t.string :uid, null: false, default: ""
## Database authenticatable
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
# etc...
When I do rails c and type User there I get a huge list of its columns
User(id: integer, provider: string, uid: string, encrypted_password:
string, reset_password_token: string, reset_password_sent_at: datetime,
remember_created_at: datetime, sign_in_count: integer,
current_sign_in_at: datetime, last_sign_in_at: datetime,
current_sign_in_ip: string, last_sign_in_ip: string,
confirmation_token: string, confirmed_at: datetime,
confirmation_sent_at: datetime, unconfirmed_email: string...
But when I type u = User.find(1). It has only some columns id, provider, uid, created_at, updated_at.
When I do u.to_json I get {"id":1,"provider":"email","uid":"mymail@mail.com","name":"Name","surname":"Surname","email":"mymail@mail.com","created_at":"2016-08-07T18:27:24.186Z","updated_at":"2016-08-07T18:34:48.647Z"}.
It ignores fields like confirmed_at, confirmation_sent_at etc...
So, I want to add some more "hidden" columns in my model. How do I do it?
Thanks!
回答1:
Devise has BLACKLIST_FOR_SERIALIZATION constant for it. Check it in source: https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L59
You can hide columns from serialization in your Devise config at config/initializers/devise.rb with line like:
Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION.concat [:column_1, :column2]
回答2:
Do you have different fields in your database? (condirmed_at, confirmation_send_at etc..)
Something like that:
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
And others ?
Check your schema file (db/schema.rb)
来源:https://stackoverflow.com/questions/38817448/devise-hidden-fields