【发布时间】:2010-09-29 17:46:12
【问题描述】:
我想知道在 Rails 迁移中向数据库表添加记录的首选方法是哪种。我在 Ola Bini 的书(Jruby on Rails)上读到他做了这样的事情:
class CreateProductCategories < ActiveRecord::Migration
#defines the AR class
class ProductType < ActiveRecord::Base; end
def self.up
#CREATE THE TABLES...
load_data
end
def self.load_data
#Use AR object to create default data
ProductType.create(:name => "type")
end
end
这很好,很干净,但由于某种原因,不适用于最新版本的 rails...
问题是,如何使用默认数据(如用户或其他数据)填充数据库?
谢谢!
【问题讨论】:
-
这就是我所做的,请包括您收到的错误。
-
完整代码是这样的:pastie.org/pastes/251539 错误是'CreateProductCategories is not missing constant ProductType'
-
为此使用种子.rb。
标签: ruby-on-rails database migration