This project is archived and is in readonly mode.

#4578 ✓invalid
Gabe da Silveira

has_many association proxy delete method sometimes deletes too liberally

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

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

class Record < ActiveRecord::Base
  has_many :translations


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

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.

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh May 12th, 2010 @ 03:46 AM

    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 #{}" => 'cursed brake')
        brakes.each do |brake|
          puts "processing brake #{}"

    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 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.

  • Ryan Bigg

    Ryan Bigg May 13th, 2010 @ 11:48 PM

    • 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.

  • Neeraj Singh

    Neeraj Singh July 15th, 2010 @ 03:15 AM

    • 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?

  • Gabe da Silveira

    Gabe da Silveira July 15th, 2010 @ 03:25 AM

    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.

  • Neeraj Singh

    Neeraj Singh July 15th, 2010 @ 03:30 AM

    • State changed from “incomplete” to “invalid”
  • Rohit Arondekar

    Rohit Arondekar October 7th, 2010 @ 05:21 AM

    • Tag changed from activerecord associations, bugmash to activerecord, associations, bugmash

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>