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 754829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:03:32+00:00 2026-05-14T15:03:32+00:00

I’m creating a Ruby on Rails application, and I’m trying to create/login/logout users. This

  • 0

I’m creating a Ruby on Rails application, and I’m trying to create/login/logout users.

This is the schema for Users:

  create_table "users", :force => true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.text     "reputation"
    t.integer  "questions_asked"
    t.integer  "answers_given"
    t.string   "request"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "email_hash"
    t.string   "username"
    t.string   "hashed_password"
    t.string   "salt"
  end

The user’s personal information (username, first/last names, email) is populated through a POST. Other things such as questions_asked, reputation, etc. are set by the application, so should be initialized when we create new users. Right now, I’m just setting each of those manually in the create method for UsersController:

  def create
    @user = User.new(params[:user])
    @user.reputation = 0
    @user.questions_asked = 0
    @user.answers_given = 0
    @user.request = nil
    ...
  end

Is there a more elegant/efficient way of doing this?

  • 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-05-14T15:03:33+00:00Added an answer on May 14, 2026 at 3:03 pm

    params[:user] is just a hash, you could create a hash and merge it with the params like

    def create
        params[:user].merge(
            { 
                :reputation => 0,
                :questions_asked => 0,
                :answers_given => 0
                ...
            }
        )
    
        @user = User.new(params[:user])
    end
    

    You could move this to your model if you wanted to remove that code from your controller, and just add an after_create filter..

    but really if its just setting things to 0, set defaults in the database columns and you wont even have to handle it in your code..

      create_table "users", :force => true do |t|
        t.string   "first_name"
        t.string   "last_name"
        t.text     "reputation", :default => 0
        t.integer  "questions_asked", :default => 0
        t.integer  "answers_given", :default => 0
        t.string   "request"
        t.datetime "created_at"
        t.datetime "updated_at"
        t.string   "email_hash"
        t.string   "username"
        t.string   "hashed_password"
        t.string   "salt"
      end
    

    If you cannot redo your migration, use change_column_default like

    class SetDefaultsOnUsers < ActiveRecord::Migration
      def self.up
         change_column_default "users", "reputation", 0
      end
    
      def self.down
         change_column_default "users", "reputation", default
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 463k
  • Answers 463k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are strictly tied to sending valid SOAP response… May 16, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer How about using grep instead of sed? echo adf99fgl12,345qsdfm34 |… May 16, 2026 at 12:53 am
  • Editorial Team
    Editorial Team added an answer Mostly because it does eat up extra time when you… May 16, 2026 at 12:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.