0

我以这种方式定义了三个类图片、员工和产品

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

我想将图片的模型名称更改为图像。关于多态关联,我需要更新什么?

4

1 回答 1

2

首先重命名文件名:from picture.rbto image.rb,类名:from PicturetoImage和关联:from has_many :picturesto has_many :images

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

class RenamePicturesToImages < ActiveRecord::Migration
  def change
    rename_table :pictures, :images
  end
end

最后运行rake db:migrate

于 2012-10-11T16:54:29.553 回答