Ruby on rails 模型名称更改时更新Rails中的多态关联
我用这种方式定义了三个类:图片、员工和产品Ruby on rails 模型名称更改时更新Rails中的多态关联,ruby-on-rails,database,ruby-on-rails-3,polymorphic-associations,Ruby On Rails,Database,Ruby On Rails 3,Polymorphic Associations,我用这种方式定义了三个类:图片、员工和产品 Class Picture < ActiveRecord::Base belongs_to :imageable, :polymorphic => true end class Employee < ActiveRecord::Base has_many :pictures, :as => :imageable end class Product < ActiveRecord::Base has_many
Class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
类图片true
结束
类Employee:可成像
结束
类产品:可成像
结束
我想将图片的型号名称更改为图像。关于多态关联,我需要更新什么?首先将文件名:from
picture.rb
重命名为image.rb
,类名:frompicture
toimage
,关联:fromhas\u many:pictures
重命名为has\u many:images
然后创建更改图片表的迁移,如下所示:
class RenamePicturesToImages < ActiveRecord::Migration
def change
rename_table :pictures, :images
end
end
类重命名EpictureStoImages
最后运行
rakedb:migrate
谢谢。我要试一试。