aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorIvan Antropov <antropov.ivan@gmail.com>2013-11-10 12:28:54 +0700
committerIvan Antropov <antropov.ivan@gmail.com>2013-11-17 12:20:50 +0700
commitec0928076529e8f0b5a4ad58c95cfa1fe6ed5b60 (patch)
tree30ebc3fd0590552a2b175b83c5d99c6b0f347c39 /activerecord/lib/active_record
parentfa13d8e6d926c601070104c7075be39618280272 (diff)
downloadrails-ec0928076529e8f0b5a4ad58c95cfa1fe6ed5b60.tar.gz
rails-ec0928076529e8f0b5a4ad58c95cfa1fe6ed5b60.tar.bz2
rails-ec0928076529e8f0b5a4ad58c95cfa1fe6ed5b60.zip
Fix insertion of records for hmt association with scope, fix #3548
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb6
-rw-r--r--activerecord/lib/active_record/relation.rb4
2 files changed, 7 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 31b8d27892..53268372eb 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -84,12 +84,16 @@ module ActiveRecord
@through_records[record.object_id] ||= begin
ensure_mutable
- through_record = through_association.build
+ through_record = through_association.build through_scope_attributes
through_record.send("#{source_reflection.name}=", record)
through_record
end
end
+ def through_scope_attributes
+ scope.where_values_hash(through_association.reflection.name.to_s)
+ end
+
def save_through_record(record)
build_through_record(record).save!
ensure
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 6e0669a77f..5e38ed632d 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -527,9 +527,9 @@ module ActiveRecord
#
# User.where(name: 'Oscar').where_values_hash
# # => {name: "Oscar"}
- def where_values_hash
+ def where_values_hash(relation_table_name = table_name)
equalities = where_values.grep(Arel::Nodes::Equality).find_all { |node|
- node.left.relation.name == table_name
+ node.left.relation.name == relation_table_name
}
binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }]