User model:

class User < ActiveRecord::Base

attr_accessor :name, :email
has_secure_password
validates :password, presence: true, length: { minimum: 6 }

end

The problem is, in the Rails console, the attributes name, email, password, password_confirmation don't show up.

I suspect the first two are caused due to setting attr_accessor :name, :email and the latter two due to has_secure_password

But, when I call those attributes separately, they show up:

Loading development environment (Rails 4.2.0)
2.2.1 :001 > u = User.new(name: "asd", email: "[email protected]", password: "qweasd", password_confirmation: "qweasd")
 => #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: "$2a$10$a98/zxfH0zaT0Hh.xalVPOxwbJiXAkH17BiRg.sV4hw..."> 
2.2.1 :002 > u
 => #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: "$2a$10$a98/zxfH0zaT0Hh.xalVPOxwbJiXAkH17BiRg.sV4hw..."> 
2.2.1 :003 > u.name
 => "asd" 
2.2.1 :004 > u.email
 => "[email protected]" 
2.2.1 :005 > u.password
 => "qweasd" 
2.2.1 :009 > u.save
(0.2ms)  begin transaction
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
  SQL (0.4ms)  INSERT INTO "users" ("password_digest", "created_at", "updated_at") VALUES (?, ?, ?)  [["password_digest", "$2a$10$a98/zxfH0zaT0Hh.xalVPOxwbJiXAkH17BiRg.sV4hwFXp2jUiTnm"], ["created_at", "2015-06-10 02:42:22.437148"], ["updated_at", "2015-06-10 02:42:22.437148"]]
   (130.8ms)  commit transaction
 => true 
2.2.1 :010 > u
 => #<User id: 3, name: nil, email: nil, created_at: "2015-06-10 02:42:22", updated_at: "2015-06-10 02:42:22", password_digest: "$2a$10$a98/zxfH0zaT0Hh.xalVPOxwbJiXAkH17BiRg.sV4hw..."> 
2.2.1 :011 > u.name
 => "asd"

I've checked everything thoroughly and can't find what's causing this, any insights to what's actually happening under the hood would be deeply appreciated.

1

There are 1 best solutions below

10
On

password is not a column in your database users, and neither it should be. It is just an attribute. If you have used has_secure_password, and you have - Rails will automatically take the password you specified, encrypt it, and store a password_digest in the database. There would be not password column in the database.

In Rails 4.2, you don't need to write attr_accessor with your column names in your model. If you have defined them in migration, Rails will automagically create getter/setter and a bunch of other helping methods for you. So, you model User should look like following in Rails 4.2:

class User < ActiveRecord::Base

  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }
end