Skip to content

Instantly share code, notes, and snippets.

@guilherme
Last active December 11, 2015 02:39
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 guilherme/4532583 to your computer and use it in GitHub Desktop.
Save guilherme/4532583 to your computer and use it in GitHub Desktop.
class A < ActiveRecord::Base
def update_attributes_changing_type(attrs)
object = if attrs.keys.include?('type') || attrs.keys.include?(:type)
self.becomes((attrs['type'] || attrs[:type]).constantize)
else
self
end
object.class.instance_variable_set('@finder_needs_type_condition', :false)
object.update_attributes(attrs)
object.class.instance_variable_set('@finder_needs_type_condition', nil)
object
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment