aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2013-04-19 12:52:44 +0100
committerJon Leighton <j@jonathanleighton.com>2013-06-28 11:47:00 +0100
commit55193e449a377c448e43d8fec42899ea1ff93b27 (patch)
treedfda1e0a1c4ee49d7f40304277afddc26c062b13 /activerecord/lib/active_record/associations
parent4642f5487a491de283910c662a519f14c21e136f (diff)
downloadrails-55193e449a377c448e43d8fec42899ea1ff93b27.tar.gz
rails-55193e449a377c448e43d8fec42899ea1ff93b27.tar.bz2
rails-55193e449a377c448e43d8fec42899ea1ff93b27.zip
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.
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_association.rb2
1 files changed, 2 insertions, 0 deletions
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)