Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I initialize an Model object in RoR?

I'm creating a website where I want every user to start off with certain values for their attributes.

Here is the class:

class User < ActiveRecord::Base

  attr_accessible :name, 
              :email, 
              :goal, 
              :measurement, 
              :bmr_formula, 
              :fat_factor, 
              :protien_factor

end

In rails console --sandbox I'm able to change the values. But I want to start the object off with certain values.

For example, I want measurement to begin with "US", bmr_formula to begin with "Katch"...etc instead of nil.

right now, everything starts with nil.

I'll proceed to show what I've tried with the results each attempt got.



Here is what worked:

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 > user = User.new
User has been initialized!
=> #<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> 
1.9.3p125 :002 > 

Thanks for the help everyone!



Full Class:

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

Console:

1.9.3p125 :001 > user = User.new("US")
 => #<User not initialized>

Bottom of Class:

 def initialize               
    @measurement = "US"
    # bmr_formula = "katch"
    # fat_factor = 0.655
    # protien_factor = 1.25
  end

Console:

1.9.3p125 :001 > user = User.new
 => #<User not initialized> 
1.9.3p125 :002 > 

Bottom of Class:

self.@measurement = "US"

Console:

SyntaxError: /Users/Nick/Code/Rails/fitness_app/app/models/user.rb:10: syntax error, unexpected tIVAR
self.@measurement = "US"

Class:

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

Console:

1.9.3p125 :001 > user = User.new
 => #<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> 
1.9.3p125 :002 > user.measurement
=> nil 
1.9.3p125 :003 > user.bmr_formula
=> nil 
like image 992
Nick Res Avatar asked Aug 14 '12 02:08

Nick Res


1 Answers

I would use the after_initialize callback:

after_initialize :default_values
def default_values
  self.status ||= 'P'
  self.measurement = "US"
  self.bmr_formula = "katch"
  self.fat_factor = 0.655
  self.protien_factor = 1.25
end
like image 63
Dominic Bou-Samra Avatar answered Oct 12 '22 22:10

Dominic Bou-Samra