Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails: change a relationship to be polymorphic

Let's take a slightly more complex case of the example of polymorphic relationships as seen in the docs (2.9)
Suppose that initially, only a Product can have an Image; We would have something like this-

class Picture < ActiveRecord::Base
  belongs_to :product
end

class Employee < ActiveRecord::Base
  # nothing yet
end

class Product < ActiveRecord::Base
  has_many :pictures
end

After a while, we want to add the ability for an Employee to have an image also;
What kind of migrations do I need to run to make sure that the existing Products' images are preserved and the relationship is now polymorphic, like so-

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
like image 658
J. Ed Avatar asked Oct 23 '25 23:10

J. Ed


2 Answers

class AddImageableIdAndTypeToPictures < ActiveRecord::Migration
  def up
    add_column :pictures, :imageable_type, :string
    add_column :pictures, :imageable_id, :integer

    Picture.all.each do |picture| 
      picture.imageable_type = 'Product'
      picture.imageable_id = picture.product_id
      picture.save
    end
  end
end
like image 185
Pardeep Dhingra Avatar answered Oct 25 '25 14:10

Pardeep Dhingra


I would write migration like this:

remove_foreign_key  :pictures, :products
add_column :pictures, :imageable_type, :string
rename_column :pictures, :product_id, :imageable_id


Picture.update_all(imageable_type: "Product")
like image 41
pa3k Avatar answered Oct 25 '25 13:10

pa3k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!