shatyajeet
shatyajeet

Reputation: 311

Updating Polymorphic Associations in Rails on change in model name

I have three classes Picture, Employee and Product defined this way

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

I want to change the Model name of Picture to Image. What all do I need to update regarding the polymorphic associations?

Upvotes: 0

Views: 737

Answers (1)

Agis
Agis

Reputation: 33626

First rename the filename: from picture.rb to image.rb, the class name: from Picture to Image and the associations: from has_many :pictures to has_many :images.

Then create a migration that changes your pictures table, like this:

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

Finally run rake db:migrate.

Upvotes: 2

Related Questions