Initializing "new users" in Rails

Posted by mathee on Stack Overflow See other posts from Stack Overflow or by mathee
Published on 2010-04-26T01:57:52Z Indexed on 2010/04/26 2:03 UTC
Read the original article Hit count: 339

Filed under:

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails