Rails迁移 - 未定义的方法`'

时间:2015-04-03 19:16:14

标签: ruby-on-rails rails-migrations

我们正在尝试将DB列重命名为rails convention:

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

但这会引发错误:

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

尝试排除我尝试的范围/语法错误,但无济于事:

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

而且,如果你压平循环并手动调用rename_column,它似乎可以工作。

2 个答案:

答案 0 :(得分:2)

你的文件中有一个unicode空间,Ruby决定是一个方法调用。它是其中之一:http://www.fileformat.info/info/unicode/char/2002/index.htm

如果您从错误消息中复制空间并使用常规空间搜索替换,我认为应该修复它。

答案 1 :(得分:0)

在该循环中似乎有复制并粘贴en space

快速搜索并替换:

:%s/ / /g