【问题标题】:Updating Polymorphic Associations in Rails on change in model name在模型名称更改时更新 Rails 中的多态关联
【发布时间】:2012-10-11 15:16:35
【问题描述】:

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

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

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

【问题讨论】:

    标签: ruby-on-rails database ruby-on-rails-3 polymorphic-associations


    【解决方案1】:

    首先重命名文件名:从picture.rbimage.rb,类名:从PictureImage 和关联:从has_many :pictureshas_many :images

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

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

    最后运行rake db:migrate

    【讨论】:

    • 谢谢。我会试一试的。
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多