aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-10-14 13:44:32 +0100
committerJon Leighton <j@jonathanleighton.com>2010-10-14 13:44:32 +0100
commit002985fb66ae63f157db84f83520c3c256c04f77 (patch)
treebbcb831a16fabb527746afe7aa6358e61cff521e /activerecord/test
parent25acd19da5f75a425218740fbb187b18bbb060ce (diff)
downloadrails-002985fb66ae63f157db84f83520c3c256c04f77.tar.gz
rails-002985fb66ae63f157db84f83520c3c256c04f77.tar.bz2
rails-002985fb66ae63f157db84f83520c3c256c04f77.zip
Add test_has_one_through_has_one_through_with_belongs_to_source_reflection
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/nested_has_many_through_associations_test.rb15
-rw-r--r--activerecord/test/fixtures/clubs.yml4
-rw-r--r--activerecord/test/fixtures/members.yml3
-rw-r--r--activerecord/test/fixtures/memberships.yml7
-rw-r--r--activerecord/test/models/club.rb3
-rw-r--r--activerecord/test/models/member.rb2
-rw-r--r--activerecord/test/schema/schema.rb1
7 files changed, 31 insertions, 4 deletions
diff --git a/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb b/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
index 6212eed0eb..0bd19c10e0 100644
--- a/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/nested_has_many_through_associations_test.rb
@@ -26,6 +26,7 @@ require 'models/club'
require 'models/organization'
require 'models/category'
require 'models/categorization'
+require 'models/membership'
# 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
@@ -36,7 +37,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, :organizations, :categories, :categories_posts,
- :categorizations
+ :categorizations, :memberships
# Through associations can either use the has_many or has_one macros.
#
@@ -241,9 +242,19 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
assert_equal member_types(:founding), members.first.nested_member_type
end
- # TODO: has_one through
+ # has_one through
# Source: belongs_to
# Through: has_one through
+ def test_has_one_through_has_one_through_with_belongs_to_source_reflection
+ assert_equal categories(:general), members(:groucho).club_category
+
+ members = Member.joins(:club_category).where('categories.id' => categories(:technology).id)
+ assert_equal [members(:blarpy_winkup)], members
+
+ # TODO: Make this work
+ # members = Member.includes(:club_category)
+ # assert_equal categories(:general), members.first.club_category
+ end
def test_distinct_has_many_through_a_has_many_through_association_on_source_reflection
author = authors(:david)
diff --git a/activerecord/test/fixtures/clubs.yml b/activerecord/test/fixtures/clubs.yml
index 1986d28229..82e439e8e5 100644
--- a/activerecord/test/fixtures/clubs.yml
+++ b/activerecord/test/fixtures/clubs.yml
@@ -1,6 +1,8 @@
boring_club:
name: Banana appreciation society
+ category_id: 1
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_id: 2
diff --git a/activerecord/test/fixtures/members.yml b/activerecord/test/fixtures/members.yml
index 824840b7e5..f3bbf0dac6 100644
--- a/activerecord/test/fixtures/members.yml
+++ b/activerecord/test/fixtures/members.yml
@@ -6,3 +6,6 @@ some_other_guy:
id: 2
name: Englebert Humperdink
member_type_id: 2
+blarpy_winkup:
+ id: 3
+ name: Blarpy Winkup
diff --git a/activerecord/test/fixtures/memberships.yml b/activerecord/test/fixtures/memberships.yml
index eed8b22af8..60eb641054 100644
--- a/activerecord/test/fixtures/memberships.yml
+++ b/activerecord/test/fixtures/memberships.yml
@@ -18,3 +18,10 @@ other_guys_membership:
member_id: 2
favourite: false
type: CurrentMembership
+
+blarpy_winkup_crazy_club:
+ joined_on: <%= 4.weeks.ago.to_s(:db) %>
+ club: crazy_club
+ member_id: 3
+ favourite: false
+ type: CurrentMembership
diff --git a/activerecord/test/models/club.rb b/activerecord/test/models/club.rb
index 6e7cdd643a..83d6b1b15a 100644
--- a/activerecord/test/models/club.rb
+++ b/activerecord/test/models/club.rb
@@ -4,10 +4,11 @@ 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
private
def private_method
"I'm sorry sir, this is a *private* club, not a *pirate* club"
end
-end \ No newline at end of file
+end
diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb
index 44c10cc4a4..bed62f8b7f 100644
--- a/activerecord/test/models/member.rb
+++ b/activerecord/test/models/member.rb
@@ -18,4 +18,6 @@ class Member < ActiveRecord::Base
has_many :organization_member_details, :through => :member_detail
has_many :organization_member_details_2, :through => :organization, :source => :member_details
+
+ has_one :club_category, :through => :club, :source => :category
end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index 2fa9a4521e..8b9c56b895 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -113,6 +113,7 @@ ActiveRecord::Schema.define do
create_table :clubs, :force => true do |t|
t.string :name
+ t.integer :category_id
end
create_table :collections, :force => true do |t|