我有三个类图片,员工和产品以这种方式定义
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
我想将Picture的Model名称更改为Image。关于多态关联,我需要更新什么?
答案 0 :(得分:2)
首先重命名文件名:从picture.rb
到image.rb
,类名:从Picture
到Image
以及关联:从has_many :pictures
到{{ 1}}。
然后创建一个更改图片表的迁移,如下所示:
has_many :images
最后运行class RenamePicturesToImages < ActiveRecord::Migration
def change
rename_table :pictures, :images
end
end
。