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.

Regression of ticket #2888: habtm inserts duplicate rows into join table

#177

In revision r9248, there is a test_update_attributes_after_push_without_duplicate_join_table_rows

in has_and_belongs_to_many_associations_test.rb that addresses ticket http://dev.rubyonrails.org/ticke...

This test verifies that duplicate rows are not created when duplicate objects are pushed into the habtm association one by one with 'save' called in between. For example, no duplicate rows are created for the following:

developer = Developer.new("name" => "Kano")

project = SpecialProject.create("name" => "Special Project")

assert developer.save

assert_equal 1, developer.projects.size

developer = Developer.new("name" => "Kano")

project = SpecialProject.create("name" => "Special Project")

assert developer.save

assert_equal 1, developer.projects.size

However, when a list of duplicate objects are pushed into the habtm association at the same time without 'save' called in between the elements of the list, duplicate rows are introduced into the join table. For example:

developer = Developer.new("name" => "Kano")

project = SpecialProject.create("name" => "Special Project")

developer.projects << [project, project]

  1. developer.projects.size is 2 instead of 1
  2. following test will fail:

assert_equal 1, developer.projects.size

Based on what the test for ticket 2888 says, it seems like this isn't how '<<' is supposed to behave. Is this intentional for some reason? Calling save for each push could result in a lot of trips to the db.

Reported by Jerry Cheung · May 13th, 2008 @ 06:40 AM

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

Activity

  1. josh
    josh
    • State changed from new to stale
    • Tag set to activerecord, habtm

    Closing this ticket as stale. If this is still an issue for you, feel free to reopen this ticket or create a new one with an updated description. Remember those unit tests too ;)

    July 17th, 2008 @ 01:16 AM

  2. Marcin
    Marcin

    I use RoR 2.1.1, and encounter problem with double records in habtm association.

    For example: I have model Abc and Def. Abc habtm Def objects. When I do

    a = Abc.find(some_id) a.defs << Def.find(other_id) a.defs << Def.find(other_id)

    I receive two identical records in the association table.

    I think this is a bug.

    October 1st, 2008 @ 07:18 PM