This project is archived and is in readonly mode.

#3659 ✓resolved
Hates_

accepts_nested_attributes_for causes duplicate entries in join model table

Reported by Hates_ | January 5th, 2010 @ 03:19 PM

The code below works fine in 2.3.4. When creating a new instance with accepts_nested_attributes_for, I get duplicate entries in the join table (I removed the index for testing).

class Publisher < ActiveRecord::Base

has_many :publisher_users has_many :users, :through => :publisher_users

accepts_nested_attributes_for :users

end

class PublisherUser < ActiveRecord::Base
belongs_to :publisher belongs_to :user end

class User < ActiveRecord::Base

has_many :publisher_users has_one :publisher, :through => :publisher_users

end

*In console:*

Publisher.create(:name => "name", :agrees_terms => "1", :users_attributes => {"0" => {:fullname => "full name", :email => "test@test.com", :password => "password"}})

SQL Output

Publisher Create (0.2ms) INSERT INTO publishers ….
User Create (0.2ms) INSERT INTO users….

PublisherUser Create (0.2ms) INSERT INTO publisher_users (created_at, updated_at, user_id, publisher_id) VALUES('2010-01-05 15:07:07', '2010-01-05 15:07:07', 156, 5)
PublisherUser Create (0.1ms) INSERT INTO publisher_users (created_at, updated_at, user_id, publisher_id) VALUES('2010-01-05 15:07:07', '2010-01-05 15:07:07', 156, 5)

Comments and changes to this ticket

  • Hates_

    Hates_ January 8th, 2010 @ 11:46 AM

    • Tag changed from accepts_nested_attributes_for, activerecord to accepts_nested_attributes_for, activerecord, fixed

    Tested with 2-3-stable branch and this seems to have been fixed.

  • Eloy Duran

    Eloy Duran January 11th, 2010 @ 10:18 AM

    • State changed from “new” to “resolved”
    • Assigned user set to “Eloy Duran”

    I was just gonna ask that :) Thanks for the update.

  • Alessio

    Alessio April 6th, 2010 @ 03:53 PM

    Hello Richard,

    I have the same problem than you! I am with Rails 2.3.5, how have you solved this problem? Rails 2.3.5 isn't a 2-3-stable branch? (I am begginer)

    Thank you!

    Alessio

  • Yossi

    Yossi April 7th, 2010 @ 11:13 PM

    Eloy,
    I have the same problem (Rails 2.3.5)
    Can you tell me how did you resolved it?

    Thanks,
    Yossi

  • Eloy Duran

    Eloy Duran April 8th, 2010 @ 09:42 AM

    Rails 2.3.5 was released at the end of november last year: http://weblog.rubyonrails.org/2009/11/30/ruby-on-rails-2-3-5-released. So seeing as Richard confirmed it was fixed on 2-3-stable in january this year, it means the fix is not in a release yet.

    Until 2.3.6 is released, I'd advice you to do a checkout of http://github.com/rails/rails/tree/2-3-stable.

    HTH

  • Liam Morley (carpeliam)

    Liam Morley (carpeliam) June 26th, 2010 @ 05:09 PM

    • Importance changed from “” to “Low”

    Which commit was this, or which public release did this get rolled in? As I saw this in 2.3.5, upgraded to 2.3.8, and I'm still seeing it. Here is my model:

    class User < ActiveRecord::Base
      has_many :conferences, :through => :conference_attendees, :order => 'end_date DESC'
      has_many :conference_attendees
      accepts_nested_attributes_for, :conferences, :allow_destroy => true
    end
    
    class Conference < ActiveRecord::Base
      has_many :users, :through => :conference_attendees
      has_many :conference_attendees, :dependent => :destroy
      accepts_nested_attributes_for :conference_attendees, :allow_destroy => true
    end
    
    class ConferenceAttendee < ActiveRecord::Base
      belongs_to :conference
      belongs_to :user
    end
    
    @user = User.first
    # @user.id == 2
    params = {"user"=>{"conferences_attributes"=>{"0"=>{"name"=>"Conf Name", "conference_attendees_attributes"=>{"0"=>{"role"=>"Attendee", "user_id"=>"1984"}}}}}}
    @user.update_attributes params['user']
    

    When I run this in Rails 2.3.8, I see the following in my logs:

      Conference Create (0.2ms)   INSERT INTO tech_conferences (name, created_at, updated_at) VALUES('Conf Name', '2010-06-26 03:01:07', '2010-06-26 03:01:07')
      ConferenceAttendee Create (0.2ms)   INSERT INTO tech_conference_attendees (created_at, updated_at, role, user_id, conference_id) VALUES('2010-06-26 03:01:07', '2010-06-26 03:01:07', 'Attendee', 1984, 1124)
      ConferenceAttendee Create (0.7ms)   INSERT INTO tech_conference_attendees (created_at, updated_at, role, user_id, conference_id) VALUES('2010-06-26 03:01:07', '2010-06-26 03:01:07', NULL, 2, 1124)
    

    This varies slightly from the original poster. In my case, the second conference has a NULL value for the 'role' field in ConferenceAttendee, and also has the same user id as the @user although that ID was not specified in the params for ConferenceAttendee.

    Also of note: I tried changing my accepts_nested_attributes_for :conference_attendees and added a :reject_if that rejects everything, and ran this again. The first ConferenceAttendee is not created, but the second one is.

    My quick-and-dirty workaround for this right now is an after_save that deletes the record if it doesn't have a 'role' attribute, I'm just hoping this kluge doesn't get me fired. :) Please, if you've got a better temporary solution, I'm all ears.

  • Satish Chauhan

    Satish Chauhan July 7th, 2010 @ 12:33 PM

    I have the same problem (Rails 2.3.5)

    Its resolved in 2.3.8.

    any idea about the patch that resolved this problem?

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