I downloaded a rails project.
Bundled it.
Run migrations.
Now the tables are empty. Which is the best way to fill it. irb or migrations itself. ?
You can use seeds.rb to fill the tables.
Create /db/seeds.rb
5.times do |i|
Model.create(name: "xxx")
end
And run rake db:seed.
rake db:seed
Look "Migrations and Seed Data" in rails guide.
http://guides.rubyonrails.org/migrations.html#migrations-and-seed-data
Rails has a seeds feature to populate the db with initial data, check out this guide, paragraph 8
Active Record Migrations
hope that helps
Neither of those is best way to do.You should be doing CRUD operations in order to keep your Rails project up and running.
By doing so,you should be able to create,edit,update and destroy the Records.
You have to read these Guides.You need it.
Related
I created some tables using rails. Now I want to modify the structure of a few of them. I know it can be done using rails migration. But i was wondering if it would cause any anomaly in the rails app if I modify the schemas using mysql rdbms?
Doing such changes through a migration has the advantage of not losing the changes if you decide to recreate/remigrate the database.
Also it serves as documentation. Imagine if your coworker altered some tables sneakily (and then you both forgot about it).
Technically, updating schemas directly in the database should work, but don't do it.
To add to Sergio's point, you're missing a simple fact - Rails' migrations create the famous db/schema.rb file - from which your migrations pull all their data.
The importance of schema.rb is overlooked - it is one of the most crucial aspects of your application.
db/schema.rb
The schema gives all your migrations a version of your DB to change / add to. Each time you perform a migration, Rails changes the schema file to ensure it has a "blueprint" of your db stored on file.
The schema is then able to rebuild the database using such methods as rake db:schema:load (ONLY RECOMMENDED FOR NEW INSTALLS -- DELETES PREVIOUS DATA)
So whilst there's no problem setting up the db using the db's own native tools, I recommend against it. You need to keep your migrations up to speed so that Rails can build the appropriate tables from its schema.
So in my company we are slowly moving to Rails instead of PHP(Code Igniter to be precise).
So, our actual PHP App is using a Mysql DB and I'd like to connect a new Rails app to this DB but meanwhile our PHP is still running, so I can't change the DB.
I don't really know where I should start to use all the rails features (Or at least as much as possible).
There shouldn't be any harm in connecting your rails app to an existing database. You will need to watch for anything that goes against rails conventions (table names are plurals of models, for example) and either change the database (and your php app) or program around the problem in rails.
But the first step is simply to connect to the database and make models for the existing tables and see what works and what doesn't.
After that, post here with any specific problems.
As a suggestion, take a backup of your database and start out programming against that to build your application and be sure everything works safely.
Well, first of all you should setup the connection in config/database.yml and then start to generate the scaffolding (models, views and controllers) table by table (check the Rails generate command). I am not really sure if you have already generated the app though. Anyway, the generator will also generate a migration script that you obviously dont want to run as the db is already there.
Hope this helps a bit.
Anyway, some resources:
http://guides.rubyonrails.org/
http://railsapps.github.io/
There are two aspects of a Rails app to consider for this scenario:
1: the database connection
Simply put the credentials for this database into database.yml.
A model like "User" will by default attempt to find records and attribute definitions in a table called "users". ActiveRecord will assume there's an auto-incrementing integer primary key on each table. When saving records, it will attempt to write to columns called created_at and updated_at. Those are a few things to be mindful of when making and using the connection.
2: the database migrations
Rails uses migration files to manage a sequence of changes to the database structure. Under normal conditions, someone building a Rails app will be starting with an empty database.
In the case of an existing database, I would recommend making a migration something like:
class BuildLegacyDbStructure < ActiveRecord::Migration
def up
Mysql2.connection.execute_some_sql_file( # made-up function
Rails.root.join('path', 'to', 'file')
)
end
def down
# reverse those changes; bring DB down to blank state
end
end
Another option would be to disable Rails/ActiveRecord's migration-based management of the database entirely. For example Rails will generate a migration when you generate a new model. So if you have an existing users table in your PHP app, and you'd like to make a rails model to use this table, you'd run something like rails generate model User --no-migration.
I am trying to figure out how to build a rails app on top of an existing mysql db. I think the best method would just be to create a migration with the same layout as the existing db, but I am not quite sure how to do this, then connect it. I am aware of this post Building Ruby on Rails App with an existing Mysql db
but am still unsure; do I just do this but with the columns I need? Also the main answer to this question is saying that I should make my db a csv and then import it, does anyone have a tutorial or gem they recommend for that?
I have not done this exact task personally although when I modify my databases manually through my mysql client and create backup tables for example, they magically appear in my schema.rb file later down the road when I run some future migrations.
So the following post should help or at least point you in the right direction:
http://tianhsky.ueuo.com/blog/2012/02/20/generate-schema-rb-from-existing-database/
Before that, try to learn more about rails and it's conventions. Probably you'll need to adapt your database scheme.
Or you could start an application and then import the data, even by SQL or by CSV as you mentioned. Migrating data can be a tedious work, but a necessary one.
You can check this gem to see if it helps on your case, because it will depend on your actual schema.
I am writing a gem that does some complex parsing/composition of a plist file.
I thought it would make sense to use the ActiveRecord interface for this and build 'structures' as an in-memory database.
The gem works fine on its own. However, I'd like to be able to incorporate it into a Rails project. The problem is that my gem calls establish_conneciton on ActiveRecord::Base which kills any previous connection the Rails app had.
It does seem to be possible because there's a section in the ActiveRecord::Base documentation titled: Connection to multiple databases in different models
This implies you can subclass from ActiveRecord::Base and call establish_connection on that. The problem then is that you can't define your schema because ActiveRecord::Schema.define uses the connection currently set on Base.
I'm thinking that the only way to do this may be to build the tables into the Rails app the includes the gem. I'd rather not go down that route if I can help it though. Any ideas?
You can view the database setup file for my gem here, if I've not made myself clear.
Many thanks
Thanks for the comments. Andrew Marshall: In this case, my tables are temporary and intended for in-memory use only.
I figured out a solution that may be of use to anyone who stumbles upon this:
Switching connection on ActiveRecord::Schema
Thanks again.
I am current developing a big migration script, where I need to do the following:
1- Create new columns in table_1
2- Copy the values form table_2 to the new columns created in table_1
3- Delete table_2
I developed the up and down methods for this migration. My problem is that step 2 is just not copying my data correctly I need to be able to test/debug my code as it runs.
If there is a way please let me know. I am currently using Rails 2.3.8 but If there is a way for Rails 3 or 3.1 also please let me know.
Thanks a lot
To debug your data migration, you can use the ruby-debug gem and insert a breakpoint in the migration code:
require 'ruby-debug'; debugger
Then run the rake task:
rake db:migrate:redo VERSION=whatever_version_number_your_migration_has
Quick ruby-debug tutorial here.
Manipulating data in migrations is not a good idea. They should be used only to edit database structure. But you can always rename existing table, it`s columns and add new columns to it or remove some of them. List of available actions that can be performed on database via migrations can be found at http://api.rubyonrails.org/classes/ActiveRecord/Migration.html