Skip to content

Commit

Permalink
Add test_has_many_through_has_one_through
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Oct 12, 2010
1 parent 1777600 commit 6a016a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Expand Up @@ -21,6 +21,8 @@
require 'models/member'
require 'models/member_detail'
require 'models/member_type'
require 'models/sponsor'
require 'models/club'

# NOTE: Some of these tests might not really test "nested" HMT associations, as opposed to ones which
# are just one level deep. But it's all the same thing really, as the "nested" code is being
Expand All @@ -30,7 +32,7 @@
class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :authors, :books, :posts, :subscriptions, :subscribers, :tags, :taggings,
:people, :readers, :references, :jobs, :ratings, :comments, :members, :member_details,
:member_types
:member_types, :sponsors, :clubs

# Through associations can either use the has_many or has_one macros.
#
Expand Down Expand Up @@ -97,9 +99,19 @@ def test_has_many_through_has_one_with_has_one_through_source_reflection
assert_equal [member_types(:founding)], members.first.nested_member_types
end

# TODO: has_many through
# has_many through
# Source: has_one
# Through: has_one through
def test_has_many_through_has_one_through
assert_equal [sponsors(:moustache_club_sponsor_for_groucho)], members(:groucho).nested_sponsors

members = Member.joins(:nested_sponsors).where('sponsors.id' => sponsors(:moustache_club_sponsor_for_groucho).id)
assert_equal [members(:groucho)], members

# TODO: Make this work
# members = Member.includes(:nested_sponsors)
# assert_equal [sponsors(:moustache_club_sponsor_for_groucho)], members.first.nested_sponsors
end

# TODO: has_many through
# Source: has_many through
Expand Down
3 changes: 3 additions & 0 deletions activerecord/test/models/member.rb
Expand Up @@ -12,4 +12,7 @@ class Member < ActiveRecord::Base

has_many :nested_member_types, :through => :member_detail, :source => :member_type
has_one :nested_member_type, :through => :member_detail, :source => :member_type

has_many :nested_sponsors, :through => :sponsor_club, :source => :sponsor
has_one :nested_sponsor, :through => :sponsor_club, :source => :sponsor
end

0 comments on commit 6a016a5

Please sign in to comment.