Skip to content

Instantly share code, notes, and snippets.

@luckyruby
Created March 26, 2014 19:23
Show Gist options
  • Save luckyruby/9791185 to your computer and use it in GitHub Desktop.
Save luckyruby/9791185 to your computer and use it in GitHub Desktop.
class ConvertAgentNameToIds < ActiveRecord::Migration
TYPES = ['created', 'modified', 'cancelled']
def up
@agents = Hash[*User.select([:first_name, :last_name, :username]).map {|i| ["#{i.first_name} #{i.last_name}", i.username]}.flatten]
TYPES.each {|i| convert_agent(i)}
end
def down
@agents = Hash[*User.select([:first_name, :last_name, :username]).map {|i| [i.username, "#{i.first_name} #{i.last_name}"]}.flatten]
TYPES.each {|i| convert_agent(i)}
end
private
def convert_agent(type)
res = Reservation.where("#{type}_by IS NOT NULL")
res.each do |r|
agent = @agents[r.send("#{type}_by")]
r.update_attribute("#{type}_by", agent) if agent.present?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment