title: How to make active record initialize with params in Rails 4?
body: <p>I am using Ruby on Rails 4 and I have this User model:</p>
  
  <pre><code>require 'uuid'
  UUID.state_file = false
  UUID.generator.next_sequence
  
  class User &lt; ActiveRecord::Base
     attr_accessor :email, :password
     has_many :entries
  
     after_initialize do |user|
         user.entry_hash = UUID.new.generate
     end
  
  end
  </code></pre>
  
  <p>Which is based on the following DB migration:</p>
  
  <pre><code>class CreateUsers &lt; ActiveRecord::Migration
  
  def change
      create_table :users do |t|
          t.string "email"
          t.string "password"
          t.string "entry_hash"
        t.timestamps
      end
  
    end
  end
  </code></pre>
  
  <p>I want to automatically generate a uuid as a hash associated with that User.</p>
  
  <p>Using the rails console to create a new User, I do the following:</p>
  
  <p>1) I use create a User with an email and password:</p>
  
  <pre><code>irb(main):001:0&gt; u = User.create(:email=&gt;'abc@gmail.com', :password=&gt;'myPass')
     (0.1ms)  BEGIN
    SQL (0.6ms)  INSERT INTO `users` (`created_at`, `entry_hash`, `updated_at`) VALUES ('2013-12-07 22:32:28', '60744ec0-41bd-0131-fde8-3c07542e5dcb', '2013-12-07 22:32:28')
     (3.7ms)  COMMIT
  =&gt; #&lt;User id: 2, email: nil, password: nil, entry_hash: "60744ec0-41bd-0131-fde8-3c07542e5dcb", created_at: "2013-12-07 22:32:28", updated_at: "2013-12-07 22:32:28"&gt;
  </code></pre>
  
  <p>2) But this is weird. If you look at the SQL, it doesn't insert anything but the entry_hash, and the output object shows email and password as nil. However, when I try to access those properties, I get the ones I put.</p>
  
  <pre><code>irb(main):002:0&gt; u.email
  =&gt; "abc@gmail.com"
  irb(main):003:0&gt; u.id
  =&gt; 2
  irb(main):004:0&gt; u.password
  =&gt; "myPass"
  irb(main):005:0&gt; u.entry_hash
  =&gt; "60744ec0-41bd-0131-fde8-3c07542e5dcb"
  </code></pre>
  
  <p>I am very new to Ruby on Rails and I know some magical stuff goes on in the background, but can someone enlighten me as to whats going on here? I just want to create an object with parameters.</p>
  
  <p>Cheers</p>
  
  <p><strong>UPDATE:</strong>
  I fixed the problem I was having by removing the attr_accessor line. Anyone know why that made it work?</p>