Count returns 0 without querying if parent is not saved #6978

Merged
merged 1 commit into from Oct 4, 2012
Commits on Oct 3, 2012
  1. Count returns 0 without querying if parent is not saved

    Francesco Rodriguez committed Oct 3, 2012
    Patches `CollectionAssociation#count` to return 0 without querying
    if the parent record is new. Consider the following code:
    
        class Account
          has_many :dossiers
        end
    
        class Dossier
          belongs_to :account
        end
    
        a = Account.new
        a.dossiers.build
    
        # before patch
        a.dossiers.count
        # SELECT COUNT(*) FROM "dossiers" WHERE "dossiers"."account_id" IS NULL
        # => 0
    
        # after
        a.dosiers.count # fires without sql query
        # => 0
    
    Fixes #1856.