Skip to content

Commit

Permalink
Adding test_has_many_through_has_one_with_has_many_through_source_ref…
Browse files Browse the repository at this point in the history
…lection and modifying ThroughAssociationScope to make it work correctly.
  • Loading branch information
jonleighton committed Oct 12, 2010
1 parent 6107386 commit dc39ace
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
Expand Up @@ -73,7 +73,7 @@ def construct_through_joins
case left.macro
when :belongs_to
left_primary_key = left.klass.primary_key
right_primary_key = right.primary_key_name
right_primary_key = left.primary_key_name
when :has_many, :has_one
left_primary_key = left.primary_key_name
right_primary_key = right.klass.primary_key
Expand Down
Expand Up @@ -23,6 +23,7 @@
require 'models/member_type'
require 'models/sponsor'
require 'models/club'
require 'models/organization'

# 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 @@ -32,7 +33,7 @@
class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :authors, :books, :posts, :subscriptions, :subscribers, :tags, :taggings,
:people, :readers, :references, :jobs, :ratings, :comments, :members, :member_details,
:member_types, :sponsors, :clubs
:member_types, :sponsors, :clubs, :organizations

# Through associations can either use the has_many or has_one macros.
#
Expand Down Expand Up @@ -113,9 +114,25 @@ def test_has_many_through_has_one_through_with_has_one_source_reflection
# assert_equal [sponsors(:moustache_club_sponsor_for_groucho)], members.first.nested_sponsors
end

# TODO: has_many through
# has_many through
# Source: has_many through
# Through: has_one
def test_has_many_through_has_one_with_has_many_through_source_reflection
assert_equal [member_details(:groucho), member_details(:some_other_guy)],
members(:groucho).organization_member_details

members = Member.joins(:organization_member_details).
where('member_details.id' => member_details(:groucho).id)
assert_equal [members(:groucho), members(:some_other_guy)], members

members = Member.joins(:organization_member_details).
where('member_details.id' => 9)
assert members.empty?

members = Member.includes(:organization_member_details)
assert_equal [member_details(:groucho), member_details(:some_other_guy)],
members.first.organization_member_details
end

# TODO: has_many through
# Source: has_many
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/fixtures/member_details.yml
@@ -1,3 +1,8 @@
groucho:
id: 1
member_id: 1
organization: nsa
some_other_guy:
id: 2
member_id: 2
organization: nsa
2 changes: 2 additions & 0 deletions activerecord/test/models/member.rb
Expand Up @@ -15,4 +15,6 @@ class Member < ActiveRecord::Base

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

has_many :organization_member_details, :through => :member_detail
end
2 changes: 2 additions & 0 deletions activerecord/test/models/member_detail.rb
Expand Up @@ -2,4 +2,6 @@ class MemberDetail < ActiveRecord::Base
belongs_to :member
belongs_to :organization
has_one :member_type, :through => :member

has_many :organization_member_details, :through => :organization, :source => :member_details
end

0 comments on commit dc39ace

Please sign in to comment.