adg
adg

Reputation: 562

Why is ActiveRecord suppressing some columns?

When I do User.first some of the columns are missing, for example last_sign_in_at is not returned below:

irb(main):008:0> User.first    
User Load (0.5ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1 => 
#<User id: 2, email: "[email protected]", created_at: "2016-01-11 01:30:01", 
updated_at: "2020-08-17 16:11:00", change_pw: nil, authy_id: "11111111",
last_sign_in_with_authy: "2020-08-17 16:11:00", authy_enabled: true, 
last_password_change: "2020-08-12 14:36:37">

But last_sign_in_at is defined in the database (and it has a value too):

irb(main):009:0> User
=> User(id: integer, email: 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, created_at: datetime, updated_at: datetime, change_pw: boolean,
authy_id: string, last_sign_in_with_authy: datetime, authy_enabled: boolean, 
last_password_change: datetime) irb(main):010:0>

I took the SQL shown in irb above and ran it directly in the MySQL monitor and it looked ok - all the columns were displayed - which makes me think I have a configuration issue in ActiveRecord. I think it might have started happening when I upgraded Rails but I'm not really sure. My current version is:

irb(main):010:0> Rails.version
=> "6.0.3.2"

I found an article about ignored columns but that doesn't seem to be in play.

irb(main):011:0> User.ignored_columns
=> []

For what it's worth schema.rb has all the columns.

Any suggestions as to what I'm doing wrong or how to proceed with debugging? To recap, when I do User in irb I see all the columns but when I do User.first I only see some of them, for example last_sign_in_at is missing.

Upvotes: 1

Views: 72

Answers (1)

Schwern
Schwern

Reputation: 164739

This is just the output of #inspect which is only for debugging purposes, so it's not important that it's missing.

Devise has its own version of #inspect which protects certain sensitive attributes from being displayed and logged. You can see this in #serializable_hash. The removed attributes are in Devise::Models::Authenticatable::BLACKLIST_FOR_SERIALIZATION which includes last_sign_in_at.

BLACKLIST_FOR_SERIALIZATION = [:encrypted_password, :reset_password_token,
  :reset_password_sent_at, :remember_created_at, :sign_in_count,
  :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip, :last_sign_in_ip,
  :password_salt, :confirmation_token, :confirmed_at, :confirmation_sent_at,
  :remember_token, :unconfirmed_email, :failed_attempts, :unlock_token, :locked_at
]

Upvotes: 1

Related Questions