From 55193e449a377c448e43d8fec42899ea1ff93b27 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 19 Apr 2013 12:52:44 +0100 Subject: Apply default scope when joining associations. For example: class Post < ActiveRecord::Base default_scope -> { where published: true } end class Comment belongs_to :post end When calling `Comment.join(:post)`, we expect to receive only comments on published posts, since that is the default scope for posts. Before this change, the default scope from `Post` was not applied, so we'd get comments on unpublished posts. --- .../lib/active_record/associations/join_dependency/join_association.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/lib/active_record/associations') 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 b81aecb4e5..e0715d9dd1 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -106,6 +106,8 @@ module ActiveRecord ] end + scope_chain_items += [reflection.klass.send(:build_default_scope)].compact + constraint = scope_chain_items.inject(constraint) do |chain, item| unless item.is_a?(Relation) item = ActiveRecord::Relation.new(reflection.klass, table).instance_exec(self, &item) -- cgit v1.2.3