From 146edf216cd29320b13363df00dbcf66fdfba481 Mon Sep 17 00:00:00 2001 From: John Wilger Date: Fri, 3 Oct 2008 17:51:12 -0700 Subject: [PATCH] Added test case to prove bug in has_one :through using belongs_to --- .../has_one_through_associations_test.rb | 7 ++++++- activerecord/test/fixtures/clubs.yml | 3 ++- activerecord/test/models/category.rb | 1 + activerecord/test/models/club.rb | 1 + activerecord/test/models/sponsor.rb | 2 ++ activerecord/test/schema/schema.rb | 1 + 6 files changed, 13 insertions(+), 2 deletions(-) diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index 77e3cb1..282a8b5 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -1,11 +1,12 @@ require "cases/helper" +require 'models/category' require 'models/club' require 'models/member' require 'models/membership' require 'models/sponsor' class HasOneThroughAssociationsTest < ActiveRecord::TestCase - fixtures :members, :clubs, :memberships, :sponsors + fixtures :categories, :members, :clubs, :memberships, :sponsors def setup @member = members(:groucho) @@ -19,6 +20,10 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase assert_equal clubs(:moustache_club), @member.favourite_club end + def test_has_one_through_with_belongs_to + assert_equal categories(:general), sponsors(:crazy_club_sponsor_for_groucho).category + end + def test_creating_association_creates_through_record new_member = Member.create(:name => "Chris") new_member.club = Club.create(:name => "LRUG") diff --git a/activerecord/test/fixtures/clubs.yml b/activerecord/test/fixtures/clubs.yml index 1986d28..c4d407f 100644 --- a/activerecord/test/fixtures/clubs.yml +++ b/activerecord/test/fixtures/clubs.yml @@ -3,4 +3,5 @@ boring_club: moustache_club: name: Moustache and Eyebrow Fancier Club crazy_club: - name: Skull and bones \ No newline at end of file + name: Skull and bones + category: general \ No newline at end of file diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index 4e9d247..6f117fe 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -22,6 +22,7 @@ class Category < ActiveRecord::Base has_many :categorizations has_many :authors, :through => :categorizations, :select => 'authors.*, categorizations.post_id' + has_many :clubs end class SpecialCategory < Category diff --git a/activerecord/test/models/club.rb b/activerecord/test/models/club.rb index 3ddb691..85f84ff 100644 --- a/activerecord/test/models/club.rb +++ b/activerecord/test/models/club.rb @@ -4,4 +4,5 @@ class Club < ActiveRecord::Base has_many :current_memberships has_one :sponsor has_one :sponsored_member, :through => :sponsor, :source => :sponsorable, :source_type => "Member" + belongs_to :category end \ No newline at end of file diff --git a/activerecord/test/models/sponsor.rb b/activerecord/test/models/sponsor.rb index 50c2c2d..65b7dcf 100644 --- a/activerecord/test/models/sponsor.rb +++ b/activerecord/test/models/sponsor.rb @@ -1,4 +1,6 @@ class Sponsor < ActiveRecord::Base belongs_to :sponsor_club, :class_name => "Club", :foreign_key => "club_id" belongs_to :sponsorable, :polymorphic => true + belongs_to :club + has_one :category, :through => :club end \ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index ab5c7c5..8afa89f 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -87,6 +87,7 @@ ActiveRecord::Schema.define do create_table :clubs, :force => true do |t| t.string :name + t.integer :category_id end create_table :colnametests, :force => true do |t| -- 1.5.6.5