Sha256: 02e25163fab398cc43ff6db99470e3c132baa3d4fb538d28c6933befa50172ae

Contents?: true

Size: 678 Bytes

Versions: 11

Compression:

Stored size: 678 Bytes

Contents

# frozen_string_literal: true

class ReplaceProfileIdWithString < ActiveRecord::Migration[4.2]
  class Profile < ActiveRecord::Base
  end
  class User < ActiveRecord::Base
  end

  def up
    add_column :users, :profile, :string

    User.reset_column_information
    User.find_each do |user|
      user.update_attribute(:profile, Profile.find(user.profile_id).label)
    end

    remove_column :users, :profile_id
  end

  def down
    add_column :users, :profile_id, :integer

    User.reset_column_information
    User.find_each do |user|
      user.update_attribute(:profile_id, Profile.find_by(label: user.profile).id)
    end

    remove_column :users, :profile
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
publify_core-9.2.10 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.9 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.8 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.7 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.6 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.5 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.4 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.3 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.2 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.1 db/migrate/20160605103918_replace_profile_id_with_string.rb
publify_core-9.2.0 db/migrate/20160605103918_replace_profile_id_with_string.rb