This project is archived and is in readonly mode.
accepts_nested_attributes_for causes duplicate entries in join model table
-
Hates_
- 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
- State changed from new to resolved
- Assigned user set to Eloy Duran
I was just gonna ask that :) Thanks for the update.
-
Alessio
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
Eloy,
I have the same problem (Rails 2.3.5)
Can you tell me how did you resolved it?Thanks,
Yossi -
Eloy Duran
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)
- 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 INTOtech_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 INTOtech_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 INTOtech_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_attendeesand 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
I have the same problem (Rails 2.3.5)
Its resolved in 2.3.8.
any idea about the patch that resolved this problem?