向rails迁移添加索引时出错

时间:2014-10-10 15:56:56

标签: ruby-on-rails ruby-on-rails-4.1

我正在尝试使用以下命令为rails迁移添加索引:

rails generate migration add_user_id_to_pins user_id:integer:index

我收到此错误:

irb(main):014:0> rails generate migration add_user_id_to_pins user_id:integer:index
SyntaxError: (irb):14: syntax error, unexpected tLABEL
rails generate migration add_user_id_to_pins user_id:integer:index
                                                             ^
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
        from C:/Ruby200-x64/lib/ruby/gems/2.0.0/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

如果它有进一步帮助,这是我的模型: 用户类:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :pins
end

Pins课程:

class Pin < ActiveRecord::Base
  belongs_to :user
end

生成两个域类后添加了关联。用户类由Devise生成,Pin是通过调用rails generate生成的。

我看到rails guide,语法似乎正确。那么为什么会出现这个错误?

1 个答案:

答案 0 :(得分:5)

在系统控制台(终端)中运行此命令,而不是rails控制台或irb控制台。它应该工作。