Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / migrations / using_models_in_migrations.txt
1 [[models]]
2 == Using Models In Your Migrations ==
3 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.
4
5 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 because that validation is on a column that does not yet exist.
6
7 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:
8
9 [source, ruby]
10 -------------------------
11 class AddPartNumberToProducts < ActiveRecord::Migration
12 class Product < ActiveRecord::Base
13 end
14
15 def self.up
16 ...
17 end
18
19 def self.down
20 ...
21 end
22 end
23 -------------------------
24 The migration has its own minimal copy of the Product model and no longer cares about the Product model defined in the application.
25
26 === Dealing with changing models ===
27
28 For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try and use the old column information. You can force Active Record to re-read the column information with the `reset_column_information` method, for example
29
30 [source, ruby]
31 -------------------------
32 class AddPartNumberToProducts < ActiveRecord::Migration
33 class Product < ActiveRecord::Base
34 end
35
36 def self.up
37 add_column :product, :part_number, :string
38 Product.reset_column_information
39 ...
40 end
41
42 def self.down
43 ...
44 end
45 end
46 -------------------------