Skip to content

Instantly share code, notes, and snippets.

@sbfaulkner
Created February 5, 2009 06:27
Show Gist options
  • Select an option

  • Save sbfaulkner/58573 to your computer and use it in GitHub Desktop.

Select an option

Save sbfaulkner/58573 to your computer and use it in GitHub Desktop.
fix to_yaml when dumping ActiveRecord instances with nil association_proxies
module ActiveRecord
class Base
def to_yaml_with_nil_handling(opts = {})
# detect and remove any belongs_to association proxies
self.class.reflect_on_all_associations.find_all { |a| a.macro == :belongs_to }.each do |a|
ivar = "@#{a.name}"
remove_instance_variable(ivar) if instance_variable_defined?(ivar) && instance_variable_get(ivar).class.nil?
end
to_yaml_without_nil_handling(opts)
end
alias_method_chain :to_yaml, :nil_handling
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment