Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

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

#5968

With the following:

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

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

class Reader < ActiveRecord::Base
  belongs_to :post
  belongs_to :person
end
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.

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

State: committed
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Repository
    Repository
    • State changed from new to committed

    (from [93c9f4a942c622ecbe8d46ab4838bd072aef9fc0]) Fix bug where size of through association is not correct after adding a has_many association (occurs only before main object has been reloaded).

    [#5968 Size of through association is not correct after adding a has_many association state:committed]

    Signed-off-by: Santiago Pastorino santiago@wyeworks.com
    https://github.com/rails/rails/commit/93c9f4a942c622ecbe8d46ab4838b...

    November 14th, 2010 @ 05:37 PM

  2. Repository
    Repository

    (from [f8b7c74f9b7b0e9b76c03953275d98378cb1d139]) Fix bug where size of through association is not correct after adding a has_many association (occurs only before main object has been reloaded).

    [#5968 Size of through association is not correct after adding a has_many association state:committed]

    Signed-off-by: Santiago Pastorino santiago@wyeworks.com
    https://github.com/rails/rails/commit/f8b7c74f9b7b0e9b76c03953275d9...

    November 14th, 2010 @ 05:38 PM