diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-05-05 13:01:57 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-05-05 13:01:57 -0700 |
commit | dcd04e76179611a9db28c9e391aa7d6c2a5b046a (patch) | |
tree | 381b5abe1547dd94bf216bbe4dd92746eb1c6a24 /activerecord/lib | |
parent | 58a49875df63729f07a9a81d1ee349087d258df5 (diff) | |
parent | 0d8cf53296a4d7c1e6d85c533784a2607bfe3baa (diff) | |
download | rails-dcd04e76179611a9db28c9e391aa7d6c2a5b046a.tar.gz rails-dcd04e76179611a9db28c9e391aa7d6c2a5b046a.tar.bz2 rails-dcd04e76179611a9db28c9e391aa7d6c2a5b046a.zip |
Merge pull request #5494 from armstrjare/active_record_relation_keep_association_join_context_on_merge
ActiveRecord::Relation - maintain context of joined associations on merges
Diffstat (limited to 'activerecord/lib')
3 files changed, 35 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index cd366ac8b7..e3d8356f49 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -109,7 +109,7 @@ module ActiveRecord case associations when Symbol, String reflection = parent.reflections[associations.to_s.intern] or - raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it?" + raise ConfigurationError, "Association named '#{ associations }' was not found on #{parent.active_record.name}; perhaps you misspelled it?" unless join_association = find_join_association(reflection, parent) @reflections << reflection join_association = build_join_association(reflection, parent) diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index 0d7d28e458..ea4856408d 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -55,7 +55,12 @@ module ActiveRecord def find_parent_in(other_join_dependency) other_join_dependency.join_parts.detect do |join_part| - parent == join_part + case parent + when JoinBase + parent.active_record == join_part.active_record + else + parent == join_part + end end end diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb index 3f880ce5e9..c2f0a82fd3 100644 --- a/activerecord/lib/active_record/relation/merger.rb +++ b/activerecord/lib/active_record/relation/merger.rb @@ -29,7 +29,7 @@ module ActiveRecord end class Merger - attr_reader :relation, :values + attr_reader :relation, :other def initialize(relation, other) if other.default_scoped? && other.klass != relation.klass @@ -37,13 +37,17 @@ module ActiveRecord end @relation = relation - @values = other.values + @other = other + end + + def values + @other.values end def normal_values Relation::SINGLE_VALUE_METHODS + Relation::MULTI_VALUE_METHODS - - [:where, :order, :bind, :reverse_order, :lock, :create_with, :reordering] + [:where, :joins, :order, :bind, :reverse_order, :lock, :create_with, :reordering] end def merge @@ -54,6 +58,7 @@ module ActiveRecord merge_multi_values merge_single_values + merge_joins relation end @@ -84,6 +89,26 @@ module ActiveRecord end end + def merge_joins + return if values[:joins].blank? + + if other.klass == relation.klass + relation.joins!(values[:joins]) + else + joins_to_stash, other_joins = values[:joins].partition { |join| + case join + when Hash, Symbol, Array + true + else + false + end + } + + join_dependency = ActiveRecord::Associations::JoinDependency.new(other.klass, joins_to_stash, []) + relation.joins!(join_dependency.join_associations + other_joins) + end + end + def merged_binds if values[:bind] (relation.bind_values + values[:bind]).uniq(&:first) |