diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/association_relation.rb | 14 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/association.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/associations_test.rb | 13 |
4 files changed, 33 insertions, 1 deletions
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index 0330c0f37f..994cacb4f9 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -76,6 +76,7 @@ module ActiveRecord autoload :AutosaveAssociation autoload :Relation + autoload :AssociationRelation autoload :NullRelation autoload_under 'relation' do diff --git a/activerecord/lib/active_record/association_relation.rb b/activerecord/lib/active_record/association_relation.rb new file mode 100644 index 0000000000..e2976577f6 --- /dev/null +++ b/activerecord/lib/active_record/association_relation.rb @@ -0,0 +1,14 @@ +module ActiveRecord + class AssociationRelation < Relation + def initialize(klass, table, association) + super(klass, table) + @association = association + end + + private + + def exec_queries + super.each { |r| @association.set_inverse_instance r } + end + end +end diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 710babe024..608a6af16c 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -122,7 +122,11 @@ module ActiveRecord # Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the # through association's scope) def target_scope - klass.all + all = klass.all + scope = AssociationRelation.new(klass, klass.arel_table, self) + scope.merge! all + scope.default_scoped = all.default_scoped? + scope end # Loads the \target if needed and returns it. diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 95c571fd03..0f2d22a4a2 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -18,6 +18,8 @@ require 'models/ship' require 'models/liquid' require 'models/molecule' require 'models/electron' +require 'models/man' +require 'models/interest' class AssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :developers_projects, @@ -242,6 +244,17 @@ class AssociationProxyTest < ActiveRecord::TestCase david = developers(:david) assert david.projects.equal?(david.projects) end + + test "inverses get set of subsets of the association" do + man = Man.create + man.interests.create + + man = Man.find(man.id) + + assert_queries(1) do + assert_equal man, man.interests.where("1=1").first.man + end + end end class OverridingAssociationsTest < ActiveRecord::TestCase |