class CreateActivityObjectAudiences < ActiveRecord::Migration
  def change
    create_table :activity_object_audiences do |t|
      t.references :activity_object
      t.references :relation

      t.timestamps
    end

    add_foreign_key :activity_object_audiences, :activity_objects, :name => 'activity_object_audiences_on_activity_object_id'
    add_foreign_key :activity_object_audiences, :relations, :name => 'activity_object_audiences_on_relation_id'

    ActivityObjectAudience.record_timestamps = false

    ActivityObject.all.each do |ao|
      post_activity = ao.post_activity
      next if post_activity.blank?

      ao.relation_ids = post_activity.relation_ids
      ao.activity_object_audiences.each do |aud|
        aud.created_at = aud.updated_at = ao.created_at
        aud.save!
      end
    end

    ActivityObjectAudience.record_timestamps = true
  end
end

class CreateActivityObjectAudiences < ActiveRecord::Migration
  def change
    create_table :activity_object_audiences do |t|
      t.references :activity_object
      t.references :relation

      t.timestamps
    end

    add_foreign_key :activity_object_audiences, :activity_objects, :name => 'activity_object_audiences_on_activity_object_id'
    add_foreign_key :activity_object_audiences, :relations, :name => 'activity_object_audiences_on_relation_id'

    ActivityObjectAudience.record_timestamps = false

    ActivityObject.all.each do |ao|
      post_activity = ao.post_activity
      next if post_activity.blank?

      ao.relation_ids = post_activity.relation_ids
      ao.activity_object_audiences.each do |aud|
        aud.created_at = aud.updated_at = ao.created_at
        aud.save!
      end
    end

    ActivityObjectAudience.record_timestamps = true
    ActivityObject.reset_column_information
  end
end

Fix ActivityObject obsolete column info
Fix ActivityObject obsolete column info
