将seeds.rb 分成多个部分? [英] Split seeds.rb into multiple sections?

查看:57
本文介绍了将seeds.rb 分成多个部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了便于维护,我想将我的seeds.rb 文件分成多个部分;将 a.rb 中的所有 A 种子,b.rb 中的 B 等等.单独的文件位于 db/目录中的种子.rb 中.每个文件由一堆A.create"或B.create"调用组成,我想从seeds.rb 调用这些文件.

I'd like to split my seeds.rb file into multiple sections for ease of maintenance; seed all the A's in a.rb, the B's in b.rb, etc. The separate files are located in the db/ directory with seeds.rb. Each file consists of a bunch of "A.create" or "B.create" calls and I want to call those files from seeds.rb.

我试过了:

include 'a'
include 'b'

load 'a.rb'
load 'b.rb' 

在我的seeds.rb中,但是当我调用rake db:seed"时它们似乎没有被处理.这可能更像是一个直接的 ruby​​ 问题而不是 rails 问题,但为了完整起见,我在 Mac 上使用 Ruby 1.9.2 和 Rails 3.

in my seeds.rb but they don't seem to be processed when I call "rake db:seed". This is probably more of a straight ruby question than a rails question but for completeness I'm using Ruby 1.9.2 and Rails 3 on a Mac.

推荐答案

./db/seeds/my_module.rb:

module MyModule
  puts "In my_module.rb"
  # add code here
end

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

这篇关于将seeds.rb 分成多个部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆