Question: I am going though the tutorial (which I must say is a excellent resource) and I don't quite understand the following: 

> 
> In section 6.3.1 we create a password\_digest column in the db via the creating and running a migration script via :
> 
> 
> 

```
rails generate migration add_password_digest_to_users password_digest:string
bundle exec rake db:migrate
bundle exec rake db:test:prepare
bundle exec rspec spec/

```

Then on the rails console I am able to instantiate a user model object and set password\_digest on it :

```
irb(main):007:0> @user = User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil,     password_digest: nil>
irb(main):008:0> @user.password_digest = "zzzz" => "zzzz"
irb(main):009:0> @user.password_digest => "zzzz"   

```

However I can not see a password\_digest property on the User model class definition : 

```
class User < ActiveRecord::Base
  attr_accessible :email, :name

  before_save { |user| user.email = email.downcase}

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, presence: true, length: {maximum: 50}
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
end

```

I imagine Rails is doing some magic under the covers, would someone mind explaining exactly what it's doing?

Thanks!

Answer: 
You are right - what's actually going on here **is** rails magic behind the scenes.

Whenever you have a descendant of `ActiveRecord::Base` ActiveRecord will look at the database table for that class and automatically create accessors for you - they won't show up in the class definition. This seems **crazy** if you're coming from a language like C# where you had to do this kind of stuff manually before.

What ActiveRecord is doing (this is a **very** watered down explanation, the actual thing it does much more complicated) is kind of sticking the following code in your class:

```
class User < ActiveRecord::Base
  def password_digest
    @password_digest
  end

  def password_digest=(val)
    @password_digest = val
  end

end

```

The other thing to note is that it doesn't just do the creation of an attribute getter and setter for you - it mixes in some type casting based on the type of the column. Check out [this question](https://stackoverflow.com/questions/9071590/validates-something-confirmation-true-attr-accessor-confusion/9103459#9103459) for more info and some possible gotchas.

The net result of this is actually kind of a bonus, and one of the reasons I like rails: you define the column once in your database, and you get it put into your model class for free.

This pattern is common to Rails though, and you'll see it often. If you are still learning Ruby or the Rails framework and you aren't 100% sure where something comes from, don't be afraid to look more closely - so-called Rails 'magic' occurs frequently and it takes some time to not be surprised. I had this experience when I first moved to Rails from other languages.
Here are a couple of ways that a class can have a member variable that you cannot see in the class definition:

```
class ActiveRecord
  def password_digest=(val)
    @x = val
  end
  def password_digest
    @x
  end
end

class User < ActiveRecord
end

me = User.new
me.password_digest = "hello"
puts me.password_digest   #=> "hello"

```

Created dynamically at run time:

```
class User
end

User.class_eval do 
  attr_accessor :password_digest
end

me = User.new
me.password_digest = "hello"
puts me.password_digest   #=> "hello"

```

The problem I found with the rails tutorial is that:

```
1) It is extremely boring.

2) Because all you do is copy code.

```

Congratulations on getting to Chapter 6!