Created
October 19, 2012 01:17
-
-
Save danshultz/3915722 to your computer and use it in GitHub Desktop.
ActiveRecord nil patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tired of AR nil errors - this helps ensure that you always get something back if you call a bad method on nil