Skip to content

Commit

Permalink
has_many associations should accept arguments to scoped, as they did …
Browse files Browse the repository at this point in the history
…in 2.3
  • Loading branch information
willbryant committed Feb 18, 2012
1 parent 1b4e6ca commit babde2e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 3 additions & 2 deletions activerecord/lib/active_record/associations/association.rb
Expand Up @@ -83,8 +83,9 @@ def target=(target)
loaded!
end

def scoped
target_scope.merge(association_scope)
def scoped(*args)
r = target_scope.merge(association_scope)
args.empty? ? r : r.scoped(*args)
end

# The scope for this association.
Expand Down
Expand Up @@ -61,9 +61,9 @@ def proxy_association
@association
end

def scoped
def scoped(*args)
association = @association
association.scoped.extending do
association.scoped(*args).extending do
define_method(:proxy_association) { association }
end
end
Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Expand Up @@ -371,6 +371,19 @@ def test_counting_using_finder_sql
assert_equal 2, Firm.find(4).clients_using_sql.count
end

def test_scoped_without_arguments
clients = Firm.first.clients.all
assert !clients.empty?
assert_equal clients, Firm.first.clients.scoped.all
end

def test_scoped_with_arguments
clients = Firm.first.clients.all
assert !clients.empty?
assert_equal clients, Firm.first.clients.scoped(:conditions => "id IS NOT NULL").all
assert Firm.first.clients.scoped(:conditions => "id IS NULL").all.empty?
end

def test_belongs_to_sanity
c = Client.new
assert_nil c.firm
Expand Down

0 comments on commit babde2e

Please sign in to comment.