aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-18 01:11:37 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-18 01:11:37 -0500
commitbf55f28af159ccfaa0bb8e5e0b52e50e14ede406 (patch)
tree840bb1794dc832deac8757858b834c35a0e09d37 /activerecord
parentd029d50d48aa90655877749a57f316f6063fccf8 (diff)
downloadrails-bf55f28af159ccfaa0bb8e5e0b52e50e14ede406.tar.gz
rails-bf55f28af159ccfaa0bb8e5e0b52e50e14ede406.tar.bz2
rails-bf55f28af159ccfaa0bb8e5e0b52e50e14ede406.zip
add docs to CollectionAssociation#empty?
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb35
1 files changed, 32 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 24718cd4b1..7ee942d153 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -298,9 +298,38 @@ module ActiveRecord
load_target.size
end
- # Equivalent to <tt>collection.size.zero?</tt>. If the collection has
- # not been already loaded and you are going to fetch the records anyway
- # it is better to check <tt>collection.length.zero?</tt>.
+ # Returns true if the collection is empty. Equivalent to
+ # <tt>collection.size.zero?</tt>. If the collection has not been already
+ # loaded and you are going to fetch the records anyway it is better to
+ # check <tt>collection.length.zero?</tt>.
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :pets
+ # end
+ #
+ # person.pets.count # => 1
+ # person.pets.empty? # => false
+ #
+ # person.pets.delete_all
+ # person.pets.count # => 0
+ # person.pets.empty? # => true
+ #
+ # Also, you can pass a block to define a criteria. The behaviour
+ # is the same, it returns true if the collection based on the
+ # criteria is empty.
+ #
+ # person.pets
+ # # => [#<Pet name: "Wy", group: "cats">]
+ #
+ # person.pets.empty? do |pet|
+ # pet.group == 'cats'
+ # end
+ # # => false
+ #
+ # person.pets.empty? do |pet|
+ # pet.group == 'dogs'
+ # end
+ # # => true
def empty?
size.zero?
end