diff options
author | Jon Leighton <j@jonathanleighton.com> | 2013-04-19 12:52:44 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2013-06-28 11:47:00 +0100 |
commit | 55193e449a377c448e43d8fec42899ea1ff93b27 (patch) | |
tree | dfda1e0a1c4ee49d7f40304277afddc26c062b13 /activerecord/lib | |
parent | 4642f5487a491de283910c662a519f14c21e136f (diff) | |
download | rails-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')
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency/join_association.rb | 2 |
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) |