简体   繁体   English

Ruby中的Yaml数据文件在Rails上的位置

[英]Location for yaml data files in ruby on rails

I am using yaml files to provide initialisation data for the database and also to provide initialisation data for some of my services models. 我正在使用yaml文件为数据库提供初始化数据,并为某些服务模型提供初始化数据。 Where should I store these files in a ruby on rails app? 我应该将这些文件存储在Rails应用程序的ruby中的什么位置?

Based on ruby_newbie 's comment and the general lack of other responses, it seems that there is no well defined rails way for this. 基于ruby_newbie的评论和普遍缺乏其他回应,似乎没有明确定义的方法。 Reasonable locations are 合理的位置是

rails_root/data

rails_root/config/data

rails_root/db/data

You should put any data required for your application to run in the seeds file(db/seeds.rb). 您应该将应用程序运行所需的任何数据放入种子文件(db / seeds.rb)。 http://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data http://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data

If you need create a initial database state you can use seeds files in "db/seeds/". 如果需要创建初始数据库状态,则可以使用“ db / seeds /”中的种子文件。 After you can use rake to run and create initial state in your database. 之后,您可以使用rake在数据库中运行并创建初始状态。

In seeds file you can use Rails model without problems and run a follow command rake to create entries. 在种子文件中,您可以毫无问题地使用Rails模型,并运行follow命令rake创建条目。

take db:seed

You can check Rails Documentation: http://edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data 您可以查看Rails文档: http : //edgeguides.rubyonrails.org/active_record_migrations.html#migrations-and-seed-data

There may be good use case for loading fixed data into a constant without needing to store in a database. 将固定数据加载到常量而不需要存储在数据库中可能会有很好的用例。 Since this is technically fixed "data" I would suggest putting it in 由于这是技术上固定的“数据”,因此建议您将其放入

rails_root/db/yaml/

# and you'll have files like

rails_root/db/yaml/measurments.yml
rails_root/db/yaml/locations.yml

# or if you prefer
rails_root/data/yaml/

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

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