Skip to content

Instantly share code, notes, and snippets.

@radiospiel
Created September 7, 2011 15:39
Show Gist options
  • Save radiospiel/1200913 to your computer and use it in GitHub Desktop.
Save radiospiel/1200913 to your computer and use it in GitHub Desktop.
A better empty? for AR associations
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