Permalink
Browse files

Respect the default_scope on a join model when reading a through asso…

…ciation
  • Loading branch information...
1 parent 439c23d commit 16e93f2c3c3ca37d2bae9801b680b05f75c48c18 @jonleighton jonleighton committed with tenderlove Dec 12, 2010
@@ -81,7 +81,7 @@ def delete_records(records)
def find_target
return [] unless target_reflection_has_associated_record?
- with_scope(@scope) { @reflection.klass.find(:all) }
+ scoped.all
end
def has_cached_counter?
@@ -33,7 +33,7 @@ def create_through_record(new_value) #nodoc:
private
def find_target
- with_scope(@scope) { @reflection.klass.find(:first) }
+ scoped.first
end
end
end
@@ -3,6 +3,13 @@ module ActiveRecord
module Associations
module ThroughAssociationScope
+ def scoped
+ with_scope(@scope) do
+ @reflection.klass.scoped &
+ @reflection.through_reflection.klass.scoped
+ end
+ end
+
protected
def construct_find_scope
@@ -462,4 +462,8 @@ def test_size_of_through_association_should_increase_correctly_when_has_many_ass
post.people << people(:michael)
assert_equal readers + 1, post.readers.size
end
+
+ def test_has_many_through_with_default_scope_on_join_model
+ assert_equal posts(:welcome).comments, authors(:david).comments_on_first_posts
+ end
end
@@ -9,9 +9,13 @@
require 'models/minivan'
require 'models/dashboard'
require 'models/speedometer'
+require 'models/author'
+require 'models/post'
+require 'models/comment'
class HasOneThroughAssociationsTest < ActiveRecord::TestCase
- fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans, :dashboards, :speedometers
+ fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans,
+ :dashboards, :speedometers, :authors, :posts, :comments
def setup
@member = members(:groucho)
@@ -229,4 +233,8 @@ def test_value_is_properly_quoted
minivan.dashboard
end
end
+
+ def test_has_one_through_with_default_scope_on_join_model
+ assert_equal posts(:welcome).comments.first, authors(:david).comment_on_first_posts
+ end
end
@@ -26,6 +26,10 @@ def testing_proxy_target
has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'"
has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post
+ has_many :first_posts
+ has_many :comments_on_first_posts, :through => :first_posts, :source => :comments, :order => 'posts.id desc, comments.id asc'
+ has_one :comment_on_first_posts, :through => :first_posts, :source => :comments, :order => 'posts.id desc, comments.id asc'
+
has_many :thinking_posts, :class_name => 'Post', :conditions => { :title => 'So I was thinking' }, :dependent => :delete_all
has_many :welcome_posts, :class_name => 'Post', :conditions => { :title => 'Welcome to the weblog' }
@@ -1,4 +1,4 @@
class Contract < ActiveRecord::Base
belongs_to :company
belongs_to :developer
-end
+end
@@ -119,3 +119,10 @@ class PostForAuthor < ActiveRecord::Base
cattr_accessor :selected_author
default_scope lambda { where(:author_id => PostForAuthor.selected_author) }
end
+
+class FirstPost < ActiveRecord::Base
+ self.table_name = 'posts'
+ default_scope where(:id => 1)
+ has_many :comments, :foreign_key => :post_id
+ has_one :comment, :foreign_key => :post_id
+end

0 comments on commit 16e93f2

Please sign in to comment.