我想将 seeds.rb 文件分成多个部分以便于维护;将所有 A 种子放入 a.rb 中,将 B 种子放入 b.rb 中,等等。单独的文件位于 db/ 目录中,包含 seeds.rb。每个文件都包含一堆“A.create”或“B.create”调用,我想从 seeds.rb 调用这些文件。
我试过了:
include 'a'
include 'b'
and
load 'a.rb'
load 'b.rb'
在我的 seeds.rb 中,但当我调用“rake db:seed”时它们似乎没有被处理。这可能更像是一个直接的 Ruby 问题,而不是 Rails 问题,但为了完整起见,我在 Mac 上使用 Ruby 1.9.2 和 Rails 3。
In ./db/seeds/my_module.rb
:
module MyModule
puts "In my_module.rb"
# add code here
end
In ./db/seeds.rb
:
require File.expand_path('../seeds/my_module', __FILE__) # the ../ just removes `seeds.rb` filename from the path which is given by __FILE__
p "In seeds.rb"
# add code here
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)