Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Size reports incorrectly for collections when more than one item is added with build

#305

Adding more than one item to a collection using collection.build and then subsequently checking the size of the collection with the size method fails to work correctly.

This code from active_record/associations/association_collection.rb:

unsaved_records = Array(@target.detect { |r| r.new_record? })

unsaved_records.size + count_records

should read:

unsaved_records = Array(@target.select { |r| r.new_record? })

unsaved_records.size + count_records

I attach a git diff for patching the code and adding a test for this.

Reported by sds · June 2nd, 2008 @ 10:48 PM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. sds
    sds

    Actually the creation of a new array in the patched version is not needed (select already returns an array), I attach new patch containing this code:

    unsaved_records = @target.select { |r| r.new_record? }

    unsaved_records.size + count_records

    June 2nd, 2008 @ 11:23 PM

  2. Repository
    Repository
    • Tag set to activerecord, bug, patch

    (from [474d42538269a141687c7c66bef6575b4682b15d]) Ensure AssociationCollection#size considers all unsaved record. [#305 state:resolved] [sds]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

    July 2nd, 2008 @ 03:21 AM

  3. Pratik
    Pratik
    • State changed from new to resolved

    July 2nd, 2008 @ 04:21 AM