简体   繁体   English

如何运行位于Rails的db文件夹中的文件(版本2.3.18)

[英]How to run a file which is located in db folder in rails (version 2.3.18)

I am working on maintenance project which is in rails version '2.3.18'. 我正在维护Rails版本“ 2.3.18”中的维护项目。 And i am able to run the migrations but could not obtain the data. 而且我能够运行迁移,但无法获取数据。 There is no seeds.rb file in db, but there is a file named example_data.rb as: db中没有seed.rb文件,但是有一个名为example_data.rb的文件,如下所示:

module FixtureReplacement
  attributes_for :category do |c|
    c.name = "General"
    c.desc = "general"
  end
  attributes_for :price, :from => :equity do |a|
    a.etype = "dollars"
    a.exchange = '60'
  end
 ..........................
.................
end

I think this is the seed file for the project. 我认为这是该项目的种子文件。 I tried running 我尝试跑步

rake db:example_data (recollecting rake db:seed), but there is no luck. rake db:example_data(正在收集rake db:seed),但是没有运气。

Please help me, how to run this file. 请帮助我,如何运行此文件。

Sadly, that's not a seed file, but a replacement for Rails' built in fixtures called FixtureReplacement that's intended to be used in your test environment. 遗憾的是,这不是种子文件,而是Rails内置的FixtureReplacement固定装置的替代品,该固定装置打算在测试环境中使用。 It's very similar to something like Fabrication or FactoryGirl . 它与FabricationFactoryGirl之类的东西非常相似。

That file doesn't actually define any preset data, but instead gives you a cleaner way to create records with preset defaults. 该文件实际上并没有定义任何预设数据,而是为您提供了一种使用预设默认值创建记录的更简便方法。 You won't be able to simply run the file, but you could write a script that includes your Rails environment and includes FixtureReplacement . 您将无法简单地运行该文件,但是可以编写一个包含Rails环境并包含FixtureReplacement From there you can run commands to generate data as shown in FixtureReplacement 's documentation : 从那里可以运行命令以生成数据,如FixtureReplacement文档所示

# Require Rails env on this line
include FixtureReplacement
new_category # Uses defaults
new_category(name: "Something", desc: "something")
new_price # Uses defaults
# and so on

Honestly though, it might be wiser to just script something from scratch. 坦白说,从头开始编写某些脚本可能更明智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM