Skip to content

Instantly share code, notes, and snippets.

@StefanWallin
Created September 4, 2015 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanWallin/060101ae95eff0c53eda to your computer and use it in GitHub Desktop.
Save StefanWallin/060101ae95eff0c53eda to your computer and use it in GitHub Desktop.
AddFlagsToHuman
class AddFlagsToHuman < ActiveRecord::Migration
def change
# Add the flags
add_column :humen, :signed_up, :boolean, default: false
add_column :humen, :welcome_email_sent, :boolean, default: false
# Set the flags values
reversible do |dir|
dir.up do
Humen.all.select(&:signed_up?).each do |human|
human.update(signed_up: true, welcome_email_sent: true)
end
end
end
end
def signed_up?
# Todo: copy from code
end
end
@beanilsson
Copy link

current method relied on the existance of certain methods plus putting private methods inside a migration is not-done.

did this instead which works at this point:

class AddFlagsToHuman < ActiveRecord::Migration
  def self.up
    add_column :humen, :signed_up, :boolean, default: false
    add_column :humen, :welcome_email_sent, :boolean, default: false

    puts 'After migration human runner'
    Human.where.not(course_id: nil, team_id: nil, competence_id: nil, email: nil).each do |human|
      if human.update(signed_up: true, welcome_email_sent: true)
        puts "Updating #{human.email}"
      end
    end
  end

  def self.down
    remove_column :humen, :signed_up, :boolean
    remove_column :humen, :welcome_email_sent, :boolean
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment