在模型名称更改时更新Rails中的多态关联

时间:2012-10-11 15:16:36

标签: 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 :pictures, :as => :imageable
end

我想将Picture的Model名称更改为Image。关于多态关联,我需要更新什么?

1 个答案:

答案 0 :(得分:2)

首先重命名文件名:从picture.rbimage.rb,类名:从PictureImage以及关联:从has_many :pictures到{{ 1}}。

然后创建一个更改图片表的迁移,如下所示:

has_many :images

最后运行class RenamePicturesToImages < ActiveRecord::Migration def change rename_table :pictures, :images end end