aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2010-10-19 16:13:06 +0100
committerJon Leighton <j@jonathanleighton.com>2010-10-19 16:13:06 +0100
commit915ea5ea826d48107e4c1953c7a32cf26727d10e (patch)
tree6d9c1284df9ac076d2b083737374f6b884f7197b /activerecord/test
parent0ceb34295501a797c9e549c581ecee17f837f01c (diff)
downloadrails-915ea5ea826d48107e4c1953c7a32cf26727d10e.tar.gz
rails-915ea5ea826d48107e4c1953c7a32cf26727d10e.tar.bz2
rails-915ea5ea826d48107e4c1953c7a32cf26727d10e.zip
Support the :primary_key option on a through reflection in a nested through association
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/nested_has_many_through_associations_test.rb17
-rw-r--r--activerecord/test/fixtures/authors.yml2
-rw-r--r--activerecord/test/models/author.rb3
-rw-r--r--activerecord/test/models/organization.rb8
-rw-r--r--activerecord/test/schema/schema.rb2
5 files changed, 30 insertions, 2 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 c39ec5d139..3a4601b032 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
@@ -21,6 +21,7 @@ require 'models/organization'
require 'models/category'
require 'models/categorization'
require 'models/membership'
+require 'models/essay'
# 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
@@ -31,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, :sponsors, :clubs, :organizations, :categories, :categories_posts,
- :categorizations, :memberships
+ :categorizations, :memberships, :essays
# Through associations can either use the has_many or has_one macros.
#
@@ -440,6 +441,20 @@ class NestedHasManyThroughAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_nested_has_many_through_with_foreign_key_option_on_the_source_reflection_through_reflection
+ assert_equal [categories(:general)], organizations(:nsa).author_essay_categories
+
+ organizations = Organization.joins(:author_essay_categories).
+ where('categories.id' => categories(:general).id)
+ assert_equal [organizations(:nsa)], organizations
+
+ assert_equal categories(:general), organizations(:nsa).author_owned_essay_category
+
+ organizations = Organization.joins(:author_owned_essay_category).
+ where('categories.id' => categories(:general).id)
+ assert_equal [organizations(:nsa)], organizations
+ end
+
private
def assert_includes_and_joins_equal(query, expected, association)
diff --git a/activerecord/test/fixtures/authors.yml b/activerecord/test/fixtures/authors.yml
index 6f13ec4dac..832236a486 100644
--- a/activerecord/test/fixtures/authors.yml
+++ b/activerecord/test/fixtures/authors.yml
@@ -3,6 +3,8 @@ david:
name: David
author_address_id: 1
author_address_extra_id: 2
+ organization_id: No Such Agency
+ owned_essay_id: A Modest Proposal
mary:
id: 2
diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb
index 53b3b80950..dd8a20ce9b 100644
--- a/activerecord/test/models/author.rb
+++ b/activerecord/test/models/author.rb
@@ -110,6 +110,9 @@ class Author < ActiveRecord::Base
has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id
has_many :essay_categories_2, :through => :essays_2, :source => :category
+ belongs_to :owned_essay, :primary_key => :name, :class_name => 'Essay'
+ has_one :owned_essay_category, :through => :owned_essay, :source => :category
+
belongs_to :author_address, :dependent => :destroy
belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
diff --git a/activerecord/test/models/organization.rb b/activerecord/test/models/organization.rb
index 1da342a0bd..c18c28c696 100644
--- a/activerecord/test/models/organization.rb
+++ b/activerecord/test/models/organization.rb
@@ -2,5 +2,11 @@ class Organization < ActiveRecord::Base
has_many :member_details
has_many :members, :through => :member_details
+ has_many :authors, :primary_key => :name
+ has_many :author_essay_categories, :through => :authors, :source => :essay_categories
+
+ has_one :author, :primary_key => :name
+ has_one :author_owned_essay_category, :through => :author, :source => :owned_essay_category
+
scope :clubs, { :from => 'clubs' }
-end \ No newline at end of file
+end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index de3baaf4ab..c77651a782 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -44,6 +44,8 @@ ActiveRecord::Schema.define do
t.string :name, :null => false
t.integer :author_address_id
t.integer :author_address_extra_id
+ t.string :organization_id
+ t.string :owned_essay_id
end
create_table :author_addresses, :force => true do |t|