Skip to content

Commit 3436fdf

Browse files
oboxodolifo
authored andcommitted
Fix for get_ids when including a belongs_to association on a has_many association [#2896 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
1 parent 2d84f24 commit 3436fdf

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

activerecord/lib/active_record/associations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ def collection_reader_method(reflection, association_proxy_class)
13981398
primary_key = reflection.source_reflection.primary_key_name
13991399
send(through.name).select("DISTINCT #{through.quoted_table_name}.#{primary_key}").map!(&:"#{primary_key}")
14001400
else
1401-
send(reflection.name).select("#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").map!(&:id)
1401+
send(reflection.name).select("#{reflection.quoted_table_name}.#{reflection.klass.primary_key}").except(:includes).map!(&:id)
14021402
end
14031403
end
14041404
end

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,10 @@ def test_get_ids_for_unloaded_associations_does_not_load_them
900900
assert !company.clients.loaded?
901901
end
902902

903+
def test_get_ids_ignores_include_option
904+
assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
905+
end
906+
903907
def test_get_ids_for_unloaded_finder_sql_associations_loads_them
904908
company = companies(:first_firm)
905909
assert !company.clients_using_sql.loaded?

activerecord/test/models/post.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def add_joins_and_select
6969
has_many :authors, :through => :categorizations
7070

7171
has_many :readers
72+
has_many :readers_with_person, :include => :person, :class_name => "Reader"
7273
has_many :people, :through => :readers
7374
has_many :people_with_callbacks, :source=>:person, :through => :readers,
7475
:before_add => lambda {|owner, reader| log(:added, :before, reader.first_name) },

0 commit comments

Comments
 (0)