Created
March 26, 2014 19:23
-
-
Save luckyruby/9791185 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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