From feb1a720d07b0854390329df7c78a5281c813d39 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 27 May 2010 12:16:35 -0400 Subject: [PATCH] Do not cache scopes with arguments [#4708 state:resolved] --- .../associations/association_collection.rb | 6 +++++- activerecord/test/cases/named_scope_test.rb | 9 +++++++++ activerecord/test/models/post.rb | 2 ++ activerecord/test/schema/schema.rb | 1 + 4 files changed, 17 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 0dfd966..eafe576 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -411,7 +411,11 @@ module ActiveRecord end elsif @reflection.klass.scopes[method] @_named_scopes_cache ||= {} - @_named_scopes_cache[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) } + if args.blank? + @_named_scopes_cache[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) } + else + @_named_scopes_cache[method] = with_scope(construct_scope) { @reflection.klass.send(method, *args) } + end else with_scope(construct_scope) do if block_given? diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 2007f54..b6dc1e2 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -142,6 +142,15 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a end + def test_has_many_association_with_named_scope_with_time + author = authors(:david) + @original_number_of_posts = Post.count + author.posts.create(:title => 'testing scope with time', :body => 'hello world') + assert_equal @original_number_of_posts + 1 , Post.count + assert_equal 0, author.posts.created_timeline(20.years.ago).size + assert author.posts.created_timeline(20.years.since).size > 0, "author should have atleast one post which was just created" + end + def test_named_scope_with_STI assert_equal 3,Post.containing_the_letter_a.count assert_equal 1,SpecialPost.containing_the_letter_a.count diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index dd06822..f98c00f 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -14,6 +14,8 @@ class Post < ActiveRecord::Base } } + scope :created_timeline, lambda { |period| where 'created_at < ?', period } + belongs_to :author do def greeting "hello" diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index f5fba2f..5b66065 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -399,6 +399,7 @@ ActiveRecord::Schema.define do t.string :type t.integer :comments_count, :default => 0 t.integer :taggings_count, :default => 0 + t.datetime :created_at end create_table :price_estimates, :force => true do |t| -- 1.6.5.2