up
add_column :cows, :farmer_id, :integer
end
And for reversibility we define the following down method:
def self.down
remove_column :cows, :farmer_id
end
Note that the method is remove_column, which may be different from what you??™d expect
from the command your DBMS would use.
Now that your migration to add the farmer_id column is finished, you should run rake
db:migrate to make sure that our Cows can see our Farmers. If you go into the mysql console,
you can see that the changes are immediate:
mysql> describe cows;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| breed | varchar(255) | YES | | NULL | |
| born_on | datetime | YES | | NULL | |
| milkable | tinyint(1) | YES | | NULL | |
| farmer_id | int(11) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
You can also rename columns using the rename_column method, which takes as arguments
the table name and the old and new names of the column you want to rename:
rename_column :cows, :born_on, :created_on
Finally, you can also change columns with the change_column method.
Pages:
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162