Skip to content

Commit 009aa88

Browse files
Neeraj Singhjosevalim
Neeraj Singh
authored andcommitted
Eager loading an association should not change the count of children
[rails#4971 state:resolved] Signed-off-by: José Valim <[email protected]>
1 parent 59693c4 commit 009aa88

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,10 @@ def remove_duplicate_results!(base, records, associations)
18151815
when Hash
18161816
associations.keys.each do |name|
18171817
reflection = base.reflections[name]
1818+
1819+
if records.any? && reflection.options && reflection.options[:uniq]
1820+
records.each { |record| record.send(reflection.name).target.uniq! }
1821+
end
18181822

18191823
parent_records = []
18201824
records.each do |record|

activerecord/test/cases/associations_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,24 @@
1414
require 'models/parrot'
1515
require 'models/ship_part'
1616
require 'models/ship'
17+
require 'models/liquid'
18+
require 'models/molecule'
19+
require 'models/electron'
1720

1821
class AssociationsTest < ActiveRecord::TestCase
1922
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
2023
:computers, :people, :readers
2124

25+
def test_eager_loading_should_not_change_count_of_children
26+
liquid = Liquid.create(:name => 'salty')
27+
molecule = liquid.molecules.create(:name => 'molecule_1')
28+
molecule.electrons.create(:name => 'electron_1')
29+
molecule.electrons.create(:name => 'electron_2')
30+
31+
liquids = Liquid.includes(:molecules => :electrons).where('molecules.id is not null')
32+
assert_equal 1, liquids[0].molecules.length
33+
end
34+
2235
def test_loading_the_association_target_should_keep_child_records_marked_for_destruction
2336
ship = Ship.create!(:name => "The good ship Dollypop")
2437
part = ship.parts.create!(:name => "Mast")

activerecord/test/models/electron.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Electron < ActiveRecord::Base
2+
belongs_to :molecule
3+
end

activerecord/test/models/liquid.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Liquid < ActiveRecord::Base
2+
set_table_name :liquid
3+
has_many :molecules, :uniq => true
4+
end
5+

activerecord/test/models/molecule.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Molecule < ActiveRecord::Base
2+
belongs_to :liquid
3+
has_many :electrons
4+
end

activerecord/test/schema/schema.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,19 @@ def create_table(*args, &block)
618618
t.datetime :updated_at
619619
end
620620

621+
create_table :liquid, :force => true do |t|
622+
t.string :name
623+
end
624+
create_table :molecules, :force => true do |t|
625+
t.integer :liquid_id
626+
t.string :name
627+
end
628+
create_table :electrons, :force => true do |t|
629+
t.integer :molecule_id
630+
t.string :name
631+
end
632+
633+
621634
except 'SQLite' do
622635
# fk_test_has_fk should be before fk_test_has_pk
623636
create_table :fk_test_has_fk, :force => true do |t|

0 commit comments

Comments
 (0)