简体   繁体   中英

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. Where should I store these files in a ruby on rails app?

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. 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). 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/". After you can use rake to run and create initial state in your database.

In seeds file you can use Rails model without problems and run a follow command rake to create entries.

take db:seed

You can check Rails Documentation: 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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