Search code examples
ruby-on-railsrails-migrations

Rails migration - undefined method ` '


We're trying to rename the columns of DB to rails conventions:

class MakeRailsy < ActiveRecord::Migration
  def change
    # Classes
    rename_table :Classes, :classes_
    rename_table :classes_, :classes
    rename_column :classes, :ClassID, :id
    rename_column :classes, :ClassNO, :class_no
    rename_column :classes, :SE,      :se
    rename_column :classes, :EE,      :ee
    rename_column :classes, :CE,      :ce
    rename_column :classes, :MBA,     :mba
    rename_column :classes, :CS,      :cs
    rename_column :classes, :AM,      :am
    rename_column :classes, :ESL,     :esl
    rename_column :classes, :U_G,     :u_g

    %w(ClassName DepartmentID SectionNumber InstructorID Units Location 
       DaysAndTimes Notes Description).each do |column|
      rename_column :classes, column, column.underscore
    end

But this throws an error:

-- rename_column(:classes, "ClassName", "class_name")
   -> 0.0089s
--  ([])
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
undefined method ` ' for #<MakeRailsy:0x007ffa718e1160>/Users/kellyprice/git/transcript_archives/db/migrate/20150401225004_make_railsy.rb:19:in `block in change'    
/Users/kellyprice/git/transcript_archives/db/migrate/20150401225004_make_railsy.rb:18:in `each'
/Users/kellyprice/git/transcript_archives/db/migrate/20150401225004_make_railsy.rb:18:in `change'
NoMethodError: undefined method ` ' for #<MakeRailsy:0x007ffa718e1160>
/Users/kellyprice/git/transcript_archives/db/migrate/20150401225004_make_railsy.rb:19:in `block in change'
/Users/kellyprice/git/transcript_archives/db/migrate/2015040122500

To try and rule out scope/syntax errors I tried, to no avail:

self.rename_column(:classes, column, column.underscore)

And, if you flatten the loop and call rename_column manually, it seems to work.


Solution

  • You've got a unicode space in your file that Ruby has decided is a method call. It's one of these: http://www.fileformat.info/info/unicode/char/2002/index.htm

    If you copy the space from the error message and search replace with a regular space, I think it should fix it.