Ruby on Rails. Predefined data installation in DB - ruby-on-rails

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.

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/

How to manage schema in a different database from within a rails app

I have a simple Rails app which populates into a certain database and there are few migration scripts. I would like to create a different set of model classes and actually migrate the schema pertaining to those files into a different database. How should we do that? Will I be able to do this from the same rails app?
Well, change the database name in database.yml , then follow the usual steps,
bundle exec rake db:setup should do the work.
Or you could create a seperate database_2.yml and then make a database.rake file and follow the steps here

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

Testing rails application with a I18N database backend

I use Rails 2.3 i18n with a database backend plugin :
http://github.com/dylanz/i18n_backend_database
This stores my translations and locales in two DB tables. What would be the best way to get these tables working with my tests? I'm guessing I could write a rake task that would copy the tables from the development DB to the test DB.
Any suggestions?
You could put the data in a seeds.rb file and run that task when loading your test environment. The benefit of this is that you'll also have some way of regaining a basic data structure if you, for some reason, wipe your computer.
One thing you could try is using fixtures for this. Do a google search for db:fixtures:dump or db:fixtures:export_all Rolling own your own implementation should be pretty easy as well.

Running DB Migrations from application

I have a rails application where each user has a separate database. (taking Joel Spolsky's advice on this). I want to run DB migrations from the rails application to create a new database and tables for this user.
What is the easiest way to do this?
Maybe the db migration is not the best for this type of thing. Thanks!
It would be nice if it could be a completely automated process. The following process would be ideal.
A user signs up on our site to use this web app
Migrations are run to create this users database and get tables setup correctly
Is there a way of calling a rake task from a ruby application?
To answer part of your question, here's how you'd run a rake task from inside Rails code:
require 'rake'
load 'path/to/task.rake'
Rake::Task['foo:bar:baz'].invoke
Mind you, I have no idea how (or why) you could have one database per user.
We use seperate configuration files for each user. So in the config/ dir we would have roo.database.yml which would connect to my personal database, and I would copy that over the database.yml file that is used by rails.
We were thinking of expanding the rails Rakefile so we could specify the developer as a environment variable, which would then select a specfic datbase configuration, allowing us to only have one database.yml file. We haven't done this though as the above method works well enough.
Actually I have discovered a good way to run DB migrations from an application:
ActiveRecord::Migrator.migrate("db/migrate/")

Resources