@@ -53,17 +53,17 @@ First, include the extensions module and add the `integer_type` option to the a
5353``` ruby
5454class Picture < ActiveRecord ::Base
5555 include PolymorphicIntegerType ::Extensions
56- belongs_to :imageable , polymorphic: true , : integer_type => true
56+ belongs_to :imageable , polymorphic: true , integer_type: true
5757end
5858
5959class Employee < ActiveRecord ::Base
6060 include PolymorphicIntegerType ::Extensions
61- has_many :pictures , as: :imageable , : integer_type => true
61+ has_many :pictures , as: :imageable , integer_type: true
6262end
6363
6464class Product < ActiveRecord ::Base
6565 include PolymorphicIntegerType ::Extensions
66- has_many :pictures , as: :imageable , : integer_type => true
66+ has_many :pictures , as: :imageable , integer_type: true
6767end
6868```
6969
@@ -83,43 +83,41 @@ If you want to migrate from a polymorphic association that is already a string y
8383class PictureToPolymorphicIntegerType < ActiveRecord ::Migration
8484
8585 def up
86- execute <<-SQL
87- ALTER TABLE pictures
88- ADD COLUMN new_imageable_type INTEGER
89- SQL
86+ change_table :pictures do |t |
87+ t.integer :new_imageable_type
88+ end
9089
9190 execute <<-SQL
9291 UPDATE reminders
9392 SET new_imageable_type = CASE imageable_type
94- WHEN ' Employee' THEN 1
95- WHEN ' Product' THEN 2
96- END
97- SQL
98- execute <<-SQL
99- ALTER TABLE pictures
100- DROP COLUMN imageable_type,
101- CHANGE COLUMN new_imageable_type imageable_type INTEGER
93+ WHEN ' Employee' THEN 1
94+ WHEN ' Product' THEN 2
95+ END
10296 SQL
97+
98+ change_table :pictures , :bulk => true do |t |
99+ t.remove :imageable_type
100+ t.rename :new_imageable_type , :imageable_type
101+ end
103102 end
104103
105104 def down
106- execute <<-SQL
107- ALTER TABLE pictures
108- ADD COLUMN new_imageable_type VARCHAR (255 )
109- SQL
105+ change_table :pictures do |t |
106+ t.string :new_imageable_type
107+ end
110108
111109 execute <<-SQL
112110 UPDATE picture
113111 SET new_imageable_type = CASE imageable_type
114- WHEN 1 THEN ' Employee'
115- WHEN 2 THEN ' Product'
116- END
117- SQL
118- execute <<-SQL
119- ALTER TABLE picture
120- DROP COLUMN imageable_type,
121- CHANGE COLUMN new_imageable_type imageable_type VARCHAR (255 )
112+ WHEN 1 THEN ' Employee'
113+ WHEN 2 THEN ' Product'
114+ END
122115 SQL
116+
117+ change_table :pictures , :bulk => true do |t |
118+ t.remove :imageable_type
119+ t.rename :new_imageable_type , :imageable_type
120+ end
123121 end
124122end
125123```
0 commit comments