title: How do I initialize an Model object in RoR?
body: <p>I'm creating a website where I want every user to start off with certain values for their attributes. </p>
  
  <p>Here is the class:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
    attr_accessible :name, 
                :email, 
                :goal, 
                :measurement, 
                :bmr_formula, 
                :fat_factor, 
                :protien_factor
  
  end
  </code></pre>
  
  <p>In rails console --sandbox I'm able to change the values. But I want to start the object off with certain values. </p>
  
  <p>For example, I want measurement to begin with "US", bmr_formula to begin with "Katch"...etc instead of nil.</p>
  
  <p>right now, everything starts with nil. </p>
  
  <p>I'll proceed to show what I've tried with the results each attempt got.</p>
  
  <hr>
  
  <hr>
  
  <p><strong>Here is what worked:</strong></p>
  
  <pre><code>after_initialize do 
     self[:measurement] = "US" 
     self[:bmr_formula] = "katch"
     self[:fat_factor] = 0.655
     self[:protein_factor] = 1.25
     puts "User has been initialized!"
  end
  
  
  1.9.3p125 :001 &gt; user = User.new
  User has been initialized!
  =&gt; #&lt;User id: nil, name: nil, email: nil, goal: nil, measurement: "US", bmr_formula: 
  "katch", fat_factor: 0, protein_factor: 0, created_at: nil, updated_at: nil&gt; 
  1.9.3p125 :002 &gt; 
  </code></pre>
  
  <p><strong>Thanks for the help everyone!</strong></p>
  
  <hr>
  
  <hr>
  
  <p>Full Class:</p>
  
  <pre><code>attr_accessible :name, 
                :email, 
                :goal, 
                :measurement, 
                :bmr_formula, 
                :fat_factor, 
                :protien_factor
  
  
  def initialize(measurement)                
  @measurement = measurement
  # bmr_formula = "katch"
  # fat_factor = 0.655
  # protien_factor = 1.25
    end
  </code></pre>
  
  <p>Console:</p>
  
  <pre><code>1.9.3p125 :001 &gt; user = User.new("US")
   =&gt; #&lt;User not initialized&gt;
  </code></pre>
  
  <hr>
  
  <p>Bottom of Class:</p>
  
  <pre><code> def initialize               
      @measurement = "US"
      # bmr_formula = "katch"
      # fat_factor = 0.655
      # protien_factor = 1.25
    end
  </code></pre>
  
  <p>Console:</p>
  
  <pre><code>1.9.3p125 :001 &gt; user = User.new
   =&gt; #&lt;User not initialized&gt; 
  1.9.3p125 :002 &gt; 
  </code></pre>
  
  <hr>
  
  <p>Bottom of Class:</p>
  
  <pre><code>self.@measurement = "US"
  </code></pre>
  
  <p>Console:</p>
  
  <pre><code>SyntaxError: /Users/Nick/Code/Rails/fitness_app/app/models/user.rb:10: syntax error, unexpected tIVAR
  self.@measurement = "US"
  </code></pre>
  
  <hr>
  
  <p>Class:</p>
  
  <pre><code>after_initialize :measurement, 
                   :bmr_formula, 
                   :fat_factor, 
                   :protien_factor
  
   def defaults
     self.measurement = "US"
     self.bmr_formula = "katch"
     self.fat_factor = 0.655
     self.protien_factor = 1.25
    end
  </code></pre>
  
  <p>Console:</p>
  
  <pre><code>1.9.3p125 :001 &gt; user = User.new
   =&gt; #&lt;User id: nil, name: nil, email: nil, goal: nil, measurement: nil, bmr_formula:    
  nil, fat_factor: nil, protien_factor: nil, created_at: nil, updated_at: nil&gt; 
  1.9.3p125 :002 &gt; user.measurement
  =&gt; nil 
  1.9.3p125 :003 &gt; user.bmr_formula
  =&gt; nil 
  </code></pre>