Al D
Al D

Reputation: 667

Rails db:seed "rake aborted! ArgumentError: wrong number of arguments (2 for 0..1)"

I'm having a nightmare trying to seed user data into my rails project. I'm using Nitrous.io and I'm able to set up a new user by typing the following into the console

rails console 

Then in the console executing the following

2.1.1 :001 > u = User.new                                                                                                                                                             
=> #<User id: nil, username: nil, email: nil, password_hash: nil, password_salt: nil, admin: nil, points: nil, leagues: nil>                                                         
2.1.1 :002 > u.username = 'testadmin'  
=> "testadmin"                                                                                                                                                                       
2.1.1 :003 > u.email = '[email protected]'                                                                                                                                           
=> "[email protected]"                                                                                                                                                              
2.1.1 :004 > u.password = 'test'                                                                                                                                                      
=> "test" 
2.1.1 :005 > u.password_confirmation = 'test'                                                                                                                                         
=> "test"                                                                                                                                                                            
2.1.1 :006 > u.admin = true                                                                                                                                                           
=> true                                                                                                                                                                              
2.1.1 :007 > u.save  
   (86.8ms)  BEGIN                                                                                                                                                                    
User Exists (87.5ms)  SELECT  1 AS one FROM "users"  WHERE "users"."email" = '[email protected]' LIMIT 1                                                                           
User Exists (86.6ms)  SELECT  1 AS one FROM "users"  WHERE "users"."username" = 'testadmin' LIMIT 1                                                                                 
SQL (91.7ms)  INSERT INTO "users" ("admin", "email", "password_hash", "password_salt", "username") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["admin", "t"], ["email", "testAdmin @test.com"], ["password_hash", "$2a$10$tVMzM58wjt.lDG6dlsoLQe4obwYzLf3UfUc2ui6MD2eTGAgwTTaQK"], ["password_salt", "$2a$10$tVMzM58wjt.lDG6dlsoLQe"], ["username", "testadmin"]]        
(89.3ms)  COMMIT                                                                                                                                                                   
=> true                                                                                                                                                                              
2.1.1 :008 > Exit

So then I go to my seeds.rb file in the project and enter the following

 User.create ({username: 'testadmin2', email: '[email protected]', password: 'test', password_confirmation: 'test', admin: true})

I then run rake db:seed in the console but get the following error:

rake aborted!                                                                                                                                                                         
ArgumentError: wrong number of arguments (2 for 0..1)                                                                                                                                 
/home/action/workspace/PAW/app/models/user.rb:39:in `initialize'                                                                                                                      
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:30:in `new'                                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:30:in `new'                                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/protected_attributes-1.0.7/lib/active_record/mass_assignment_security/persistence.rb:45:in `create'                                            
/home/action/workspace/PAW/db/seeds.rb:8:in `<top (required)>'  
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                                           
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'                                                                  
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                                           
/home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:543:in `load_seed'      
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'                                     
Tasks: TOP => db:seed                                                                                                                                                                 
(See full trace by running task with --trace)   

I'm new to ruby and rails so I'm probably doing something stupid but I've been at it the last two hours and I can't figure it out at all. The reason I want to seed data in like this is because I want to set up a few administrators that have access to certain pages (all working fine). To do this I need to set admin = true. If I set up new users through the website I don't get the option to set a user as an admin. I could do it through the console as per above but it takes ages. Seeding would be much quicker and I'd also like to know what I've been doing wrong. I'll give you some other code below. Thanks for looking (and hopefully helping)

user.rb

  class User < ActiveRecord::Base
    attr_accessible :email, :username, :password, :password_confirmation
    attr_accessor :password
    before_save :encrypt_password

    validates_confirmation_of :password
    validates_presence_of :password, :on => :create
    validates_presence_of :email, :on => :create
    validates_presence_of :username, :on => :create
    validates_uniqueness_of :email
    validates_uniqueness_of :username

    def self.authenticate_by_email(email, password)
      user = find_by_email(email)
      if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
      else
        nil
      end
    end

    def self.authenticate_by_username(username, password)
      user = find_by_username(username)
      if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
      else
        nil
      end
    end

    def encrypt_password
      if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
      end
    end

    def initialize(attributes = {})
      super # must allow the active record to initialize!
      attributes.each do |name, value|
        send("#{name}=", value)
      end
    end    
  end

authentication_controller.rb

  class AuthenticationController < ApplicationController
    def sign_in
      @user = User.new
    end

    def login
      username_or_email = params[:user][:username]
      password = params[:user][:password]

      if username_or_email.rindex('@')
        email=username_or_email
        user = User.authenticate_by_email(email, password)
      else
        username=username_or_email
        user = User.authenticate_by_username(username, password)
      end

      if user
        session[:user_id] = user.id
        flash[:notice] = 'Welcome' 
        redirect_to :root
      else
        flash.now[:error] = 'Unknown user. Please check your username and password.'

        # Assign user to instance variable for the `sign_in` view!
        @user = User.new(params[:user]) 

        render :action => "sign_in"
      end
    end

    def signed_out
      session[:user_id] = nil
      flash[:notice] = "You have been signed out."
    end

    def new_user
      @user = User.new
    end

    def register
      @user = User.new(params[:user])

      if @user.valid?
        @user.save
        session[:user_id] = @user.id
        flash[:notice] = 'Welcome.'
        redirect_to :root
      else
        render :action => "new_user"
      end
    end

  end

Question now answered. See below.

Upvotes: 1

Views: 2701

Answers (1)

Paritosh Piplewar
Paritosh Piplewar

Reputation: 8132

  1. dont do this in model (User.rb)

    def initialize(attributes = {})
      super # must allow the active record to initialize!
      attributes.each do |name, value|
      send("#{name}=", value)
     end
    end
    

    This is not required. Rails automatically creates getter and setter method.

  2. And, while creating record with create, there is no need to wrap parameters with {}.

Upvotes: 3

Related Questions