Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add an example

  • Loading branch information...
commit 5ad2bce12d91786486c7f476f06a5139e12046ff 1 parent bb52108
@fcheung fcheung authored
View
25 railties/doc/guides/migrations/anatomy_of_a_migration.txt
@@ -1,6 +1,6 @@
== Anatomy Of A Migration ==
-Before I dive into the details of a migration, here is a typical example:
+Before I dive into the details of a migration, here are a few examples of the sorts of things you can do:
[source, ruby]
------------------------
@@ -22,6 +22,29 @@ end
This migration adds a table called `products` with a string column called `name` and a text column called `description`. A primary key column called `id` will also be added, however since this is the default we do not need to ask for this. The timestamp columns `created_at` and `updated_at` which Active Record populates automatically will also be added. Reversing this migration is as simple as dropping the table.
+Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields:
+
+[source, ruby]
+------------------------
+class AddReceiveNewsletterToUsers < ActiveRecord::Migration
+ def self.up
+ change_table :users do |t|
+ t.boolean :receive_newsletter, :default => false
+ end
+ User.update_all ["receive_newsletter = ?", true]
+ end
+
+ def self.down
+ remove_column :users, :receive_newsletter
+ end
+end
+------------------------
+
+This migration adds an `receive_newsletter` column to the `users` table. We want it to default to `false` for new users, but existing users are considered
+to have already opted in, so we use the User model to set the flag to true for existing users.
+
+NOTE: Some <<models,caveats>> apply to using models in your migrations.
+
=== Migrations are classes
A migration is a subclass of ActiveRecord::Migration that implements two class methods: +up+ (perform the required transformations) and +down+ (revert them).
View
2  railties/doc/guides/migrations/foreign_keys.txt
@@ -1,5 +1,5 @@
-== Active Record and Referential Integrity ==
[[foreign_key]]
+== Active Record and Referential Integrity ==
The Active Record way is that intelligence belongs in your models, not in the database. As such features such as triggers or foreign key constraints, which push some of that intelligence back into the database are not heavily used.
Validations such as `validates_uniqueness_of` are one way in which models can enforce data integrity. The `:dependent` option on associations allows models to automatically destroy child objects when the parent is destroyed. These cannot however guarantee referential integrity and so some people augment them with foreign key constraints.
View
2  railties/doc/guides/migrations/using_models_in_migrations.txt
@@ -1,5 +1,5 @@
+[[models]]
== Using Models In Your Migrations ==
-
When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done but some caution should be observed. Consider for example a migration that uses the Product model to update a row in the corresponding table. Alice later updates the Product model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with `rake db:migrate`, including the one that used the Product model. When the migration runs the source is up to date and so the Product model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues.
Frequently I just want to update rows in the database without writing out the SQL by hand: I'm not using anything specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example:
Please sign in to comment.
Something went wrong with that request. Please try again.