From 5fd7ca212f9b4d47cd62f0a312a236521ef149aa Mon Sep 17 00:00:00 2001 From: Lonnie Warpup Date: Mon, 8 Mar 2010 22:17:26 -0500 Subject: [PATCH] Add failing test case for named scopes and self referential relations. --- .../has_and_belongs_to_many_associations_test.rb | 2 +- .../associations/has_many_associations_test.rb | 9 +++++++++ activerecord/test/fixtures/projects.yml | 5 +++++ activerecord/test/models/project.rb | 4 ++++ activerecord/test/schema/schema.rb | 1 + 5 files changed, 20 insertions(+), 1 deletions(-) diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 5e8b2ca..a88207e 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -741,7 +741,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(join_base, :developers, nil) projects = Project.send(:select_limited_ids_list, {:order => 'developers.created_at'}, join_dep) assert !projects.include?("'"), projects - assert_equal %w(1 2), projects.scan(/\d/).sort + assert_equal %w(1 2 3), projects.scan(/\d/).sort end def test_scoped_find_on_through_association_doesnt_return_read_only_records diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index eee80b2..7579173 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1130,5 +1130,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase client = firm.clients_using_primary_key.create!(:name => 'test') assert_equal firm.name, client.firm_name end + + def test_named_scope_does_not_scope_self_referential_relationships + parent = projects(:action_controller) + child = projects(:action_controller_subproject) + assert_equal parent.id, child.parent_id + Project.roots.destroy_all + assert_nil Project.find_by_id(parent.id) + assert_nil Project.find_by_id(child.id) + end end diff --git a/activerecord/test/fixtures/projects.yml b/activerecord/test/fixtures/projects.yml index 02800c7..9daea0f 100644 --- a/activerecord/test/fixtures/projects.yml +++ b/activerecord/test/fixtures/projects.yml @@ -5,3 +5,8 @@ action_controller: active_record: id: 1 name: Active Record + +action_controller_subproject: + id: 3 + name: Active Controller Subproject + parent_id: 2 diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index f25b2dd..9e8852a 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -15,6 +15,10 @@ class Project < ActiveRecord::Base :after_remove => Proc.new {|o, r| o.developers_log << "after_removing#{r.id}"} has_and_belongs_to_many :well_payed_salary_groups, :class_name => "Developer", :group => "developers.salary", :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" + belongs_to :parent, :class_name => "Project", :foreign_key => "parent_id" + has_many :children, :class_name => "Project", :foreign_key => "parent_id", :dependent => :destroy + named_scope :roots, :conditions => { :parent_id => nil } + attr_accessor :developers_log def after_initialize diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 984c5ba..ffd950a 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -368,6 +368,7 @@ ActiveRecord::Schema.define do create_table :projects, :force => true do |t| t.string :name t.string :type + t.references :parent end create_table :readers, :force => true do |t| -- 1.6.5.2