rahulserver
rahulserver

Reputation: 11215

Error while adding index to rails migration

I am trying to add an index to a rails migration using below command:

rails generate migration add_user_id_to_pins user_id:integer:index

and I get this error:

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>'

If it helps further, here are my models: User Class:

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:

class Pin < ActiveRecord::Base
  belongs_to :user
end

The associations were added after generating the two domain classes. The user class was generated by Devise and Pin was generated by calling rails generate.

I saw rails guide and syntax seems correct. So why is this error popping up?

Upvotes: 1

Views: 159

Answers (1)

danielM
danielM

Reputation: 2512

Run this command in your system console (terminal) instead of the rails console or the irb console. It should work then.

Upvotes: 5

Related Questions