我创建了一个设计模型
迁移是:
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...当我执行 rails c 并键入 User 时,我会看到其列的巨大列表
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...但是当我键入 u = User.find(1)时.它只有一些列 id,provider,uid,created_at,updated_at .
But when I type u = User.find(1). It has only some columns id, provider, uid, created_at, updated_at.
当我执行 u.to_json 时,我得到 {"id":1,"provider":"email","uid":"mymail@mail",名称:"名称,"姓:"姓,"电子邮件:" mymail@mail," created_at:" 2016-08-07T18:27:24.186Z," updated_at:" 2016-08-07T18:34:48.647Z} .
它会忽略诸如 confirmed_at , confirmation_sent_at 等字段...
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?
谢谢!
推荐答案Devise具有 BLACKLIST_FOR_SERIALIZATION 常量.在源中检查它: github/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L59
Devise has BLACKLIST_FOR_SERIALIZATION constant for it. Check it in source: github/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L59
您可以使用以下行在 config/initializers/devise.rb 的Devise配置中隐藏序列化列,例如:
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]
更多推荐
设计隐藏字段
发布评论