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.

has_many association proxy delete method sometimes deletes too liberally

#4578

Really nasty bug bit me where I was doing something like this:

class Record < ActiveRecord::Base
  has_many :translations
end

...

record.translations.each do |translation|
  record.translations.delete(translation) if translation.is_bad?
end

if translation is a new record (ie. no id), then all new records would be deleted from the association.

this ticket is just a placeholder for now, I'll write up a failing test case and patch when I have some time.

Reported by Gabe da Silveira · May 11th, 2010 @ 10:32 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh

    I am not able to reproduce this case with rails3 edge.

    class Car < ActiveRecord::Base
      has_many :brakes
    
      def self.lab
        Car.create(:name => 'John')
        car = Car.last
        brakes = car.brakes
        brake = brakes.create(:name => 'wow')
        puts "brake id is #{brake.id}"
        brakes.build(:name => 'cursed brake')
        brakes.each do |brake|
          puts "processing brake #{brake.name}"
          car.brakes.delete(brake) 
        end
      end
    end
    

    The output of Car.lab is

    SQL (0.4ms) INSERT INTO "cars" ("name") VALUES ('John')
    Car Load (0.3ms) SELECT "cars". FROM "cars" ORDER BY cars.id DESC LIMIT 1 SQL (0.4ms) INSERT INTO "brakes" ("car_id", "name") VALUES (8, 'wow') brake id is 13
    SQL (0.5ms) INSERT INTO "brakes" ("car_id", "name") VALUES (8, 'airblad') Brake Load (0.3ms) SELECT "brakes".
    FROM "brakes" WHERE ("brakes".car_id = 8) processing brake wow
    SQL (0.3ms) UPDATE "brakes" SET "car_id" = NULL WHERE ("brakes"."car_id" = 8 AND "brakes"."id" IN (13)) processing brake cursed brake
    => [#]

    Everything looks okay to me.

    May 12th, 2010 @ 03:46 AM

  2. Ryan Bigg
    Ryan Bigg
    • Tag changed from activerecord associations to activerecord associations, bugmash
    • State changed from new to incomplete

    Please provide a failing test case and patch for this issue.

    May 13th, 2010 @ 11:48 PM

  3. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    @Gabe Let's say that I have added a new record to collection using build. Now I want to delete that record. All I have to do is collection.delete(record) and then new object previously added to collection gets deleted. This behavior looks okay to me.

    If your proposal is accepted then how would anyone delete any new record from collection?

    July 15th, 2010 @ 03:15 AM

  4. Gabe da Silveira
    Gabe da Silveira

    Actually I wasn't proposing anything, what I was experiencing was a straight-up bug. However, I eventually tracked this down to a condition in my application, so there is in fact no bug here. Please make this ticket invalid.

    July 15th, 2010 @ 03:25 AM

  5. Neeraj Singh
    Neeraj Singh
    • State changed from incomplete to invalid

    July 15th, 2010 @ 03:30 AM

  6. Rohit Arondekar
    Rohit Arondekar
    • Tag changed from activerecord associations, bugmash to activerecord, associations, bugmash

    October 7th, 2010 @ 05:21 AM