How to convert an sqlite database to JSON - ruby-on-rails

How do you convert an sqlite database (generated by Rails) to a collection of JSON objects? Is there a set of scripts/ a ruby gem that does that, or is it necessary to code something from scratch?

I'm not sure that I fully understand the question, but here's a stab.
If you have a collection called Widgets, you can call Widgets.all.to_json This is trivial to do in a Rails Console session.
You can then simply output that to where every you like, such as a text file.
Other ways would be to use a db dump task via rake rake -T will show you the available commands.

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/

Ruby on Rails. Predefined data installation in DB

I am new with Ruby on Rails and have some issues with it. I am writing my application on Rails and i have to install some pre-defined data in my Database to use it in my application. This data is read-only and never change (for example users and roles. There are no use cases to create new users and roles).
So i have to install this data when my application runs first time. I tried to use Migrations to solve this problem, but my manager told me that it is not a right way, cause migrations are usually used to define DB structure changes and not to install any data in DB.
Can you please help me and tell better way to install my pre-defined data using Ruby on Rails?
You'll be able to achieve this using the seeds functionality:
To add initial data after a database is created, Rails has a built-in 'seeds' feature that makes the process quick and easy. This is especially useful when reloading the database frequently in development and test environments. It's easy to get started with this feature: just fill up db/seeds.rb with some Ruby code, and run rake db:seed:
#db/seeds.rb
User.create name: "test", description: "info"
$ rake db:seed
--
If you wanted static data, you'll be able to use a gem such as the config gem -- which gives you the ability to allocate values in config/settings.yml:
This data is then accessible as Settings.company...
You should use seeds. See this rails cast
You could also look at Dibber, that allows you to define your seeds via YML files.
What you are looking to do is seed the database. Rails comes with a db/seeds.rb that you can use to do this. There are also gems such as seed-fu that you could use.
Here is a short tutorial on seeding a database in Rails.

Where to keep JSON files in a Rails project to repopulate the database?

I'm using Nokogiri to scraping and populate the database, but the process is slow (2~3 hours), I'd like to keep these data in JSON files to easily repopulate my database during development.
It is a good practice? What is the best place to keep these files in a Rails project?
It sounds like you're talking about seed data. If you put ruby code to load this seed data in db/seeds.rb, you can then run rake db:seed to run it as mentioned in the Active Record Migrations Rails Guide.
I would probably end up making a db/seeds ordb/seed_data directory, putting the JSON files in there, and then writing ruby in db/seeds.rb that would iterate through those JSON files and load all the data in them.

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.

How go about writing standalone Ruby ActiveRecord utility in my current rails project?

I have a RoR project on my Windows 7 PC.
I want to create some Ruby code that I can execute from the cmd.exe command line that manipulates the development database (via database.yml) of the project. (I don't want to have to run my utility code via a web page.)
What is the best way to go about pulling this off? (I'm a newbie.)
I can't put the code in the test/ directory because that executes against the test database.
I tried just creating a utility.rb file under app/ but when I run it I get this:
utility.rb:5: uninitialized constant ActiveRecord (NameError)
My standalone file obviously doesn't know about the rest of the rails framework.
Any suggestions?
Rails comes with a utility to do exactly this. Instead of using ruby filename, use script/runner filename (from within the top-level directory for the Rails project), which will automatically load up your Rails environment before running the script.
However, if what you're trying to do is manipulate the database, the right answer is probably to create a migration. Most people assume that migrations are only for changing the structure of your database (adding or removing columns or tables) but they can also be a great way to add seed data or manipulate all the data in the database.
You can write your own rake task which depends on :environment and pass RAILS_ENV=development when executing it.
Nice screencast about it: screencast

Resources