From a9492d0f51aecdf4fe2e93880ef5e88ec5393d2e Mon Sep 17 00:00:00 2001 From: Lonnie Warpup Date: Fri, 19 Sep 2008 12:46:25 -0400 Subject: [PATCH] Created test case to show that named_scope is incorrectly applying itself to a relation of the same class. --- activerecord/test/cases/named_scope_test.rb | 11 ++++++++++- activerecord/test/fixtures/people.yml | 11 ++++++++++- activerecord/test/models/person.rb | 4 ++++ activerecord/test/schema/schema.rb | 6 ++++-- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 444debd..9adce89 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -5,9 +5,10 @@ require 'models/comment' require 'models/reply' require 'models/author' require 'models/developer' +require 'models/person' class NamedScopeTest < ActiveRecord::TestCase - fixtures :posts, :authors, :topics, :comments, :author_addresses + fixtures :posts, :authors, :topics, :comments, :author_addresses, :people def test_implements_enumerable assert !Topic.find(:all).empty? @@ -271,4 +272,12 @@ class NamedScopeTest < ActiveRecord::TestCase topics.size # use loaded (no query) end end + + def test_should_return_related_records_even_if_not_within_main_scope + people = Person.males.find(:all, :include => :primary_contact) + assert_not_equal people.length, 0 + people.each do |person| + assert_not_nil person.primary_contact + end + end end diff --git a/activerecord/test/fixtures/people.yml b/activerecord/test/fixtures/people.yml index d5a69e5..1d33ea0 100644 --- a/activerecord/test/fixtures/people.yml +++ b/activerecord/test/fixtures/people.yml @@ -1,6 +1,15 @@ michael: id: 1 first_name: Michael + primary_contact: david + gender: M david: id: 2 - first_name: David \ No newline at end of file + first_name: David + primary_contact: susan + gender: M +susan: + id: 3 + first_name: Susan + primary_contact: david + gender: F \ No newline at end of file diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index 430d0b3..c757e6c 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -7,4 +7,8 @@ class Person < ActiveRecord::Base has_many :jobs, :through => :references has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true] has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id' + belongs_to :primary_contact, :class_name => 'Person' + + named_scope :males, :conditions => { :gender => 'M' } + named_scope :females, :conditions => { :gender => 'F' } end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index ab5c7c5..ee945a0 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -283,8 +283,10 @@ ActiveRecord::Schema.define do end create_table :people, :force => true do |t| - t.string :first_name, :null => false - t.integer :lock_version, :null => false, :default => 0 + t.string :first_name, :null => false + t.references :primary_contact + t.string :gender, :limit => 1 + t.integer :lock_version, :null => false, :default => 0 end create_table :pets, :primary_key => :pet_id ,:force => true do |t| -- 1.6.0.1