aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-01-08 00:49:11 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-01-08 00:58:25 +0900
commitf614c5f10874f467a21c5360e764e6d14ffc9f49 (patch)
tree21ca6123ff8c753419d3331fcc49c5f8deddd792 /activerecord/lib
parenta736e82170c6731dee01cf2f20d0dcb0fd2be5c0 (diff)
downloadrails-f614c5f10874f467a21c5360e764e6d14ffc9f49.tar.gz
rails-f614c5f10874f467a21c5360e764e6d14ffc9f49.tar.bz2
rails-f614c5f10874f467a21c5360e764e6d14ffc9f49.zip
Fix deleting through records when using has_many through with `source_type`
Currently deleting through records doesn't respect `source_type`. It should not be ignored in that case. Related #23209. Fixes #24116.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/through_association.rb14
1 files changed, 6 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb
index bce2a95ce1..249eb1775b 100644
--- a/activerecord/lib/active_record/associations/through_association.rb
+++ b/activerecord/lib/active_record/associations/through_association.rb
@@ -38,24 +38,22 @@ module ActiveRecord
def construct_join_attributes(*records)
ensure_mutable
- if source_reflection.association_primary_key(reflection.klass) == reflection.klass.primary_key
+ association_primary_key = source_reflection.association_primary_key(reflection.klass)
+
+ if association_primary_key == reflection.klass.primary_key && !options[:source_type]
join_attributes = { source_reflection.name => records }
else
join_attributes = {
- source_reflection.foreign_key =>
- records.map { |record|
- record.send(source_reflection.association_primary_key(reflection.klass))
- }
+ source_reflection.foreign_key => records.map(&association_primary_key.to_sym)
}
end
if options[:source_type]
- join_attributes[source_reflection.foreign_type] =
- records.map { |record| record.class.base_class.name }
+ join_attributes[source_reflection.foreign_type] = [ options[:source_type] ]
end
if records.count == 1
- Hash[join_attributes.map { |k, v| [k, v.first] }]
+ join_attributes.transform_values!(&:first)
else
join_attributes
end