1

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?

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yeah! It worked. I am using it on windows and tried to run from git bash after calling rails console. That time it failed. But when ran directly from git bash without calling rails console, it runs! Thanks Man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.