From 0695eb6dac751e0e46bb9fd0ac5d70c0a94a86b1 Mon Sep 17 00:00:00 2001 From: Arun Agrawal Date: Fri, 3 Jun 2011 08:28:00 +0530 Subject: [PATCH] Test added for namedscope target. --- activerecord/test/cases/named_scope_test.rb | 8 ++++++++ activerecord/test/models/comment.rb | 3 ++- activerecord/test/models/post.rb | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 34188e4915..2afe3c8f32 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -456,6 +456,14 @@ class NamedScopeTest < ActiveRecord::TestCase end end + def test_scopes_to_get_newest + post = posts(:welcome) + old_last_comment = post.comments.newest + new_comment = post.comments.create(:body => "My new comment") + assert_equal new_comment, post.comments.newest + assert_not_equal old_last_comment, post.comments.newest + end + def test_scopes_are_reset_on_association_reload post = posts(:welcome) diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 2a4c37089a..43650c0427 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -6,7 +6,8 @@ class Comment < ActiveRecord::Base scope :for_first_author, :joins => :post, :conditions => { "posts.author_id" => 1 } - + scope :created + belongs_to :post, :counter_cache => true has_many :ratings diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index f2ab7b053e..affa37b02d 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -36,6 +36,10 @@ class Post < ActiveRecord::Base def find_most_recent find(:first, :order => "id DESC") end + + def newest + created.last + end end has_many :author_favorites, :through => :author