Where to put files that will be read in a Rails app? - ruby-on-rails

I'm developing a Rails application and within that application I developed a Rake task that will read entries from a file and store them into the DB. Producing the code was no problem, but I'd like to know, where do I place the file that is read? Is there a convention for that, if yes, what is it?
I know I could have used the seed.rb file but is it ok, by the standards, to load and read a file from there?
Thanks in advance!

Yes, put the data you wish to load in the db/seeds.rb file and to load it run rake db:seed. This is what this file was designed to do.

I don't think there's a hard and fast Rails convention for this case. When it comes to seed data I put mine in a subfolder of db.

The yaml_db plugin dumps/loads content from/to the database from the file
rails_root/db/data.yml i'm not sure if this is by convention, however, the content is db related making the db folder an appropriate choice

Where to put stuff in Rails is a problem that I've been working around for a while. The question is whether your file can fit into one of the existing concerns. For instance, is it configuration information?
Anyway, perhaps a similar fit would be the schema.rb, which is put in the db directory. Do not modify the schema.rb with your data, of course: I'm merely suggesting that the db directory, or a subdirectory, might be a place to put your file(s).
On the other hand, if you don't see any directories that hold anything similar -- it's not one of the main categories in app, nor any of the main categories above that -- then you can just make up a name and use that.

Related

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/

Rails: upload a file in a fixture to populate the db (seed data)

I've been looking for a week now. I'm using Rails 3.
I have a document section in my app and I'd like to populate the database with fake data. So far it was super easy: just add a .yml file in the Fixture folder and that's it.
Now what I'd like is to have a fixture file (.yml) that would populate the DB with documents when I rake db:fixtures:load. I just don't know how to achieve this. I've been looking to the fixture_file_upload function but I don't think it's the way to go.
Any idea? Thanks in advance.
Edit: to be perfectly clear, I'd like to upload a document that I list in my Document fixture. So when I rake db:fixtures:load, the document is actually uploaded.
Normally loading seed data is done using rake db:seed which just executes your db/seed.rb. There you can do anything you want.
If you want to reuse your fixtures you can just load them manually:
require 'active_record/fixtures'
ActiveRecord::Fixtures.create_fixtures(Rails.root.join('test/fixtures'),
'your_yml_file')
Pass the FIXTURES Environment Variable
Assuming that you have test/fixtures/documents.yml and that you don't mind clobbering the data currently in the documents table, you can load your fixture with:
rake db:fixtures:load FIXTURES=documents
If desired, you can also pass the appropriate RAILS_ENV to load your data into something other than your development database, such as test or production. Make sure you back up your database first, though. A typo could wipe out your current data set.
Are you installing fixture data for tests, or using test/fixtures to load fake development data? If the former is the case, it's hard because you don't want the test runner to be uploading a bunch of files between every test. If the latter is the case, then you ought to use rake db:seeds instead. That runs db/seeds.rb in the context of your Rails app. Use an environment flag to load the fake data in development only. Then you can use the API of your attachment library to "upload" the files.

writing spec for seeds.rb

In a project, the seeds.rb is getting complicated. We have a seeds/ directory with yml files containing data to be loaded. It should work on re-runs and it should not duplicate records (this is allowed in model). Well, the seeds.rb should be tested, it is used to update production. I'm not getting my head around this one though, wondering wether or not to move the logic to lib/ and write specs as normal. What would you do?
rails 3 with rspec.
I think you should try out the seed-fu gem:
https://github.com/mbleigh/seed-fu
It doesn't generate duplicate records and can help maintain the consistency of the data.

Editing a YAML file inside a Rails App

I'm trying to use a yaml config file as a very simple flat file db for a small rails app. The app has a single user, so I just need a way to store a username and password. The only thing is that I'd like to be able to edit the username and password while still inside the app and not require an app restart (so I can't load the YAML file inside an initializer...).
Any ideas on how I could accomplish this? I'm not married to the idea of using YAML, so if you have a better suggestion I'm all ears!
You really are better off using a database for this sort of thing, because it's how Rails is designed to work. The Rails default database is SQLite 3, which is a high-performance, reliable single file database.
Don't fight the defaults—use the right tool for the job.
You might wanna try iye for yaml editing on the fly. No DB needed, saves directly to file! Potentially you would just need something that tracks for file changes in your rails app and reloads your yaml file.
Here is the project page for iye: https://github.com/firmafon/iye
http://rubyforge.org/projects/rbyaml
http://yaml4r.sourceforge.net/doc/
(http://www.yaml.org/)

Good place for data loading scripts that rely on rails?

In your experience, where is the best place to place scripts that run data loading jobs, but which rely on rails? In my project they are in the model folder, but that adds a lot of code to the model folder and won't rails load it all into memory when the server is run (unnecessarily)? The lib/ folder looks good, but those don't have rails access unless you manually specify that in the scripts. Any clean solution here?
Are you talking jobs that you fire off via rake? (then tasks/)
Or are you talking putting data into the Rails app, then maybe you want something like the data_migration plugin.
What do you mean by 'data loading jobs'? If they are scripts that manipulate the database, put them in db/.
rake db:seed would be the best imo
put your script in db/seeds.rb

Resources