Skip to content

Instantly share code, notes, and snippets.

@Sylvain303
Last active August 29, 2015 14:08
Show Gist options
  • Save Sylvain303/46ead55ee995d56788e4 to your computer and use it in GitHub Desktop.
Save Sylvain303/46ead55ee995d56788e4 to your computer and use it in GitHub Desktop.
change name ActiveRecord can be used in migration to rename required records, here for redmine
def change(type, old_name, new_name)
case type
when :role
klass = Role
field = :name
when :group
klass = Group
field = :lastname
else
raise "unsupported type:#{type}"
end
old_o = klass.where(field.to_s => old_name).first
new_o = klass.where(field.to_s => new_name).first
assign = field.to_s + '='
case
when (new_o and old_o.nil?)
return :allready
when (new_o.nil? and old_o)
old_o.method(assign.to_sym).call(new_name)
old_o.save
when (new_o.nil? and old_o.nil?)
raise "no old record"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment