设计隐藏的字段

时间:2016-08-07 18:53:07

标签: ruby-on-rails devise

我创建了一个设计模型

迁移是:

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时,我会得到一个巨大的列列表

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

当我u.to_json时,我得到{"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"}

它会忽略confirmed_atconfirmation_sent_at等字段......

所以,我想在我的模型中添加一些“隐藏”列。我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:1)

Devise有BLACKLIST_FOR_SERIALIZATION不变。检查来源:https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L59

您可以在config/initializers/devise.rb的Devise配置中隐藏序列化中的列,其行如下:

Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION.concat [:column_1, :column2]

答案 1 :(得分:0)

您的数据库中是否有不同的字段? (3condirmed_at等。)

类似的东西:

confirmation_send_at

还有其他人吗?

检查您的架构文件(db / schema.rb)