ruby - Rails find and replace instances of attribute after rename_column -
i've looked around , haven't found elegant solution this, figured i'd ask.
i in middle of refactoring couple database tables keep naming scheme continuous throughout application. process this:
create migration rename bunch of columns:
class renamesomeattributes < activerecord::migration def change rename_column :some_tables, :old_name, :new_name end end
save record of instances of :old_name before replace them in rest of code. (optional)
$ grep -r old_name app lib spec >> find-replace-history.txt
find , replace string in models, views, controllers, javascripts, stylesheets, specs, rake tasks etc.
$ grep -rl old_name app lib spec | xargs sed -r 's/old_name/new_name/g'
however, method useful when i'm working attribute names specific table adjusting. more general attribute names (:name, :description etc), forced manually edit files avoid replacing wrong table's attribute.
is there better tool should using me find every instance of attribute, , replace when attributes applies specific table? don't mind adjusting assets separately, because won't know table referring to. however, rails should able determine if given attribute pertains table adjusting in of ruby files.
i don't think there's such tool. have manually you're doing now. however, if old attribute name still in use somewhere, there's way prevent breaking code.
in sometable
model, implement method_missing
method
def method_missing(method, *arguments, &block) if method.to_s == "old_name" self.new_name else super end end
to understand how method_missing
works, check out this article recommend should implement respond_to?
method.
you might want add following method_missing
method above (add after if
statement).
rails.logger.debug caller.join("\n")
this show code still references old attribute name. hope helps.
Comments
Post a Comment