Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:35:59+00:00 2026-06-09T13:35:59+00:00

I’m creating a website where I want every user to start off with certain

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:36:00+00:00Added an answer on June 9, 2026 at 1:36 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.