This project is archived and is in readonly mode.

#177 ✓stale
Jerry Cheung

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

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

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.

Comments and changes to this ticket

  • josh

    josh July 17th, 2008 @ 01:16 AM

    • 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 ;)

  • Marcin

    Marcin October 1st, 2008 @ 07:18 PM

    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.

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages