diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-04-10 17:43:59 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-04-10 17:44:32 -0300 |
commit | a7a180bd8158a0a2151f0711b874a7495820d6e0 (patch) | |
tree | 826babcdecdf06bda24b56b10543422596d9140c /activerecord/lib | |
parent | 632d9d22375dd79614ce731eb42875b65112b987 (diff) | |
parent | ec0928076529e8f0b5a4ad58c95cfa1fe6ed5b60 (diff) | |
download | rails-a7a180bd8158a0a2151f0711b874a7495820d6e0.tar.gz rails-a7a180bd8158a0a2151f0711b874a7495820d6e0.tar.bz2 rails-a7a180bd8158a0a2151f0711b874a7495820d6e0.zip |
Merge pull request #12829 from iantropov/issue_insert_via_hmt_scope_3548
Fix insertion of records for hmt association with scope
Conflicts:
activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 4 |
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 64bc98c642..c2eff0d8ce 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -83,12 +83,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 4d37ac6e2b..787f55e2e7 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -529,9 +529,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] }] |