This project is archived and is in readonly mode.

#5968 ✓committed
Jason Cheow

Size of through association is not correct after adding a has_many association

Reported by Jason Cheow | November 14th, 2010 @ 06:12 AM

With the following:

class Post < ActiveRecord::Base
  has_many :readers
  has_many :people, :through => :readers

class Person < ActiveRecord::Base
  has_many :readers
  has_many :posts, :through => :readers

class Reader < ActiveRecord::Base
  belongs_to :post
  belongs_to :person
post = Post.create
post.people << Person.create
post.readers.size        # Returns 2, when 1 is expected.
post.reload.readers.size # Returns 1 as expected.

This happens because ActiveRecord::Associations::HasManyThroughAssociation#insert_record has an extraneous line that adds the same through record after creating it.

A patch with a failing test and the fix is attached.

Comments and changes to this ticket

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=""></a>

People watching this ticket