Rails迁移继承不起作用

时间:2013-10-02 17:35:19

标签: ruby-on-rails ruby activerecord migration

我有一个经常发生的迁移类型的基类:

class AddEventsBaseMigration < ActiveRecord::Migration
  #ITEMS = []

  def up
    #add the items
  end

  def down
    #remove the items that were added.
  end
end

然后,当需要添加新内容时,我会创建一个继承的迁移:

class AddEvents0930 < AddEventsBaseMigration  
  ITEMS = [ ... ]
end

当我尝试运行此迁移时,出现此错误:

rake aborted!
uninitialized constant AddEventsBaseMigration
C:/.../Source/db/migrate/20131002152826_add_events_0930.rb:1
c:0:in `migrate'
Tasks: TOP => db:migrate:up

我在这里做错了什么?

Ruby: 1.8.7(2013-06-27 patchlevel 374)[i386-mingw32]

Rails: 3.2.13

编辑:我忘记提到我已尝试过:

require "add_events_base_migration"
require "./add_events_base_migration"
require "/db/migrate/add_events_base_migration"

我收到错误:no such file to load -- /db/migrate/add_events_base_migration

1 个答案:

答案 0 :(得分:0)

你应该尝试:

require_relative "add_events_base_migration"

例如,如果您在&#34; test&#34;中有单元测试类。目录,以及测试下的数据&#34; test / data&#34;目录,那么你可以在测试用例中使用这样的一行:

require_relative "data/customer_data_1" 

如果您使用的是Ruby 1.8,则可能需要使用此库:

https://github.com/steveklabnik/require_relative