新手问题 迁移数据库时的 wrong number of arguments (4 for 2..3) 问题

pcjjdy · December 04, 2014 · Last by ken replied at December 04, 2014 · 2166 hits

想创建一个注册页面,注册需要的信息是 studentsid name email。bundle exec rake db:migrate 是发生问题

C:\Users\pcjjdy\Desktop\final project>bundle exec rake db:migrate
==  AddIndexToUsersEmail: migrating ===========================================
-- add_index(:studentsid, :users, :email, {:unique=>true})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

wrong number of arguments (4 for 2..3)C:/Users/pcjjdy/Desktop/final project/db/m
igrate/20141022140717_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

db\migrate\20141022140717_add_index_to_users_email 代码如下

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :studentsid, :users, :email, unique: true
  end
end

db\migrate\20141130233939_create_users.rb 代码如下

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :studentsid
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end

app\controllers\users_controller.rb 代码如下

class UsersController < ApplicationController
     def show
       @user = User.find(params[:id])
       @microposts = @user.microposts
     end

     def new
       @user = User.new
     end

      def create
       secure_params = params.require(:user).permit(:studentsid, :name, :email, 
        :password, :password_confirmation)
       @user = User.new(secure_params)
       if @user.save
          sign_in @user
          flash[:success] = "Thank you for your registing!"
          redirect_to @user
       else
          render 'new'
       end
     end
end

add_index(table_name, column_name, options = {})

add_index 没有指定 table_name

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :studentsid
    add_index :users, :email
  end
end
You need to Sign in before reply, if you don't have an account, please Sign up first.