Rails 5 model objects not showing all Devise attributes

前端 未结 3 852
天命终不由人
天命终不由人 2021-01-18 06:53

I am using Rails 5 API with devise. I have a User model. Schema looks like this.

create_table \"users\", force: :cascade do |t|
    t.string   \         


        
相关标签:
3条回答
  • 2021-01-18 07:07

    This is probably because devise does not expose their internal attributes.

    So to get all attributes you can use .attributes (documented here) which returns a hash, on which you can call to_json:

    user = User.find(1)
    user.attributes.to_json # => contains all fields like reset_password_token etc.
    
    0 讨论(0)
  • 2021-01-18 07:13

    Try attributes method

    User.first.attributes
    
    0 讨论(0)
  • 2021-01-18 07:17

    Devise restricts attributes like encrypted_password so that the critical information doesn't get exposed in API calls. So to override this, you need to override serializable_hash method.

    def serializable_hash(options = nil) 
      super(options).merge(encrypted_password: encrypted_password) 
    end
    

    This is not a Rails 5 specific feature but a Devise feature to protect your attributes.

    Hope that helps!

    0 讨论(0)
提交回复
热议问题