1

I have a store model that has many products with a has_many :through relationship.

I have this working with accepts_nested_attributes, but the result is that rails is making duplicate associates.

I don't have anything special going on it is a very simple app.

Any ideas on why duplicates associates are getting created?

thenengah
  • 42,557
  • 33
  • 113
  • 157
  • please post your code you write so far. – Salil Apr 04 '10 at 05:34
  • let me work on it a little more and then I will post. I was just checking if anyone knows about a bug – thenengah Apr 04 '10 at 06:22
  • Does this come after a page refresh by any chance? – Lukas Apr 05 '10 at 15:04
  • @lukas, id does not come after a page refresh. – thenengah Apr 07 '10 at 09:23
  • I changed the association to just a has_many instead of with :through => and that fixed the problem. It was creating double entries with a nested nested attribute. My code was not very good so I didn't add much else to this question. You can just ignore it. – thenengah Apr 07 '10 at 09:25

2 Answers2

1

look at answer : how to avoid duplicates in a has_many :through relationship? here :

add :uniq => true to the has_many :through

class Blog < ActiveRecord::Base
 has_many :blogs_readers, :dependent => :destroy
 has_many :readers, :through => :blogs_readers, :uniq => true
end

class Reader < ActiveRecord::Base
 has_many :blogs_readers, :dependent => :destroy
 has_many :blogs, :through => :blogs_readers, :uniq => true
end

class BlogsReaders < ActiveRecord::Base
  belongs_to :blog
  belongs_to :reader
end
Community
  • 1
  • 1
tal
  • 3,423
  • 1
  • 25
  • 21
0

This is a confirmed bug in Rails, with a fix set to be included in 2.3.6.

https://rails.lighthouseapp.com/projects/8994/tickets/3575-multiple-join-records-when-using-nested_attributes-in-habtm

BrendanDean
  • 285
  • 3
  • 13