1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Eager loading an association should not change the count of children

[#4971 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Neeraj Singh 2010-07-26 15:12:36 -04:00 committed by José Valim
parent 59693c4c49
commit 009aa8825b
6 changed files with 42 additions and 0 deletions

View file

@ -1815,6 +1815,10 @@ module ActiveRecord
when Hash
associations.keys.each do |name|
reflection = base.reflections[name]
if records.any? && reflection.options && reflection.options[:uniq]
records.each { |record| record.send(reflection.name).target.uniq! }
end
parent_records = []
records.each do |record|

View file

@ -14,11 +14,24 @@ require 'models/reader'
require 'models/parrot'
require 'models/ship_part'
require 'models/ship'
require 'models/liquid'
require 'models/molecule'
require 'models/electron'
class AssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
:computers, :people, :readers
def test_eager_loading_should_not_change_count_of_children
liquid = Liquid.create(:name => 'salty')
molecule = liquid.molecules.create(:name => 'molecule_1')
molecule.electrons.create(:name => 'electron_1')
molecule.electrons.create(:name => 'electron_2')
liquids = Liquid.includes(:molecules => :electrons).where('molecules.id is not null')
assert_equal 1, liquids[0].molecules.length
end
def test_loading_the_association_target_should_keep_child_records_marked_for_destruction
ship = Ship.create!(:name => "The good ship Dollypop")
part = ship.parts.create!(:name => "Mast")

View file

@ -0,0 +1,3 @@
class Electron < ActiveRecord::Base
belongs_to :molecule
end

View file

@ -0,0 +1,5 @@
class Liquid < ActiveRecord::Base
set_table_name :liquid
has_many :molecules, :uniq => true
end

View file

@ -0,0 +1,4 @@
class Molecule < ActiveRecord::Base
belongs_to :liquid
has_many :electrons
end

View file

@ -618,6 +618,19 @@ ActiveRecord::Schema.define do
t.datetime :updated_at
end
create_table :liquid, :force => true do |t|
t.string :name
end
create_table :molecules, :force => true do |t|
t.integer :liquid_id
t.string :name
end
create_table :electrons, :force => true do |t|
t.integer :molecule_id
t.string :name
end
except 'SQLite' do
# fk_test_has_fk should be before fk_test_has_pk
create_table :fk_test_has_fk, :force => true do |t|