Skip to content

Instantly share code, notes, and snippets.

@danshultz
Created October 19, 2012 01:17
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 danshultz/3915722 to your computer and use it in GitHub Desktop.
Save danshultz/3915722 to your computer and use it in GitHub Desktop.
ActiveRecord nil patch
def nil.method_missing(method, *args)
(@@activeRecordClasses ||= ObjectSpace.each_object(Class).find_all { |c|
c.respond_to?(:ancestors) && c != ActiveRecord::Base && c.ancestors.include?(ActiveRecord::Base)
}).choice.order("rand()").first || self.method_missing(method, *args)
rescue
self.method_missing(method, *args)
end
@danshultz
Copy link
Author

Tired of AR nil errors - this helps ensure that you always get something back if you call a bad method on nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment