Created
September 7, 2011 15:39
-
-
Save radiospiel/1200913 to your computer and use it in GitHub Desktop.
A better empty? for AR associations
This file contains hidden or 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
class ActiveRecord::Associations::AssociationCollection | |
# Does the AssociationCollection contains entries? | |
def empty?(*args) | |
if fetch_first_or_last_using_find?(args) | |
if !args.last.is_a?(Hash) | |
args.push :select => "1" | |
elsif (!args.last[:select]) | |
args.last = { :select => "1" }.update(args.last) | |
end | |
find(:first, *args).nil? | |
else | |
load_target unless loaded? | |
@target.first(*args).nil? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment