From b689834bcf2730353d066277f43047f10abb8d30 Mon Sep 17 00:00:00 2001 From: Bodaniel Jeanes Date: Sun, 26 Sep 2010 22:17:18 +1000 Subject: Initial nested_has_many_through support [#1152] --- activerecord/test/models/author.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 34bfd2d881..94810e2f34 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -83,14 +83,20 @@ class Author < ActiveRecord::Base has_many :author_favorites has_many :favorite_authors, :through => :author_favorites, :order => 'name' - has_many :tagging, :through => :posts # through polymorphic has_one - has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many - has_many :tags, :through => :posts # through has_many :through + has_many :tagging, :through => :posts # through polymorphic has_one + has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many + has_many :tags, :through => :posts # through has_many :through (on source reflection + polymorphic) + has_many :distinct_tags, :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name" has_many :post_categories, :through => :posts, :source => :categories + has_many :books + has_many :subscriptions, :through => :books + has_many :subscribers, :through => :subscriptions # through has_many :through (on through reflection) + has_many :distinct_subscribers, :through => :subscriptions, :source => :subscriber, :select => "DISTINCT subscribers.*", :order => "subscribers.nick" + has_one :essay, :primary_key => :name, :as => :writer - belongs_to :author_address, :dependent => :destroy + belongs_to :author_address, :dependent => :destroy belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" scope :relation_include_posts, includes(:posts) -- cgit v1.2.3 From 14c4881f9c7bf4eae61e548542ee309c013e1fca Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 30 Sep 2010 22:01:03 +0100 Subject: Prevent test_has_many_through_a_has_many_through_association_on_through_reflection failing for me due to ordering of the results --- activerecord/test/models/author.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 94810e2f34..1efb4fc095 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -84,14 +84,14 @@ class Author < ActiveRecord::Base has_many :favorite_authors, :through => :author_favorites, :order => 'name' has_many :tagging, :through => :posts # through polymorphic has_one - has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many + has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many TODO: Why is the :source needed? has_many :tags, :through => :posts # through has_many :through (on source reflection + polymorphic) has_many :distinct_tags, :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name" has_many :post_categories, :through => :posts, :source => :categories has_many :books has_many :subscriptions, :through => :books - has_many :subscribers, :through => :subscriptions # through has_many :through (on through reflection) + has_many :subscribers, :through => :subscriptions, :order => "subscribers.nick" # through has_many :through (on through reflection) has_many :distinct_subscribers, :through => :subscriptions, :source => :subscriber, :select => "DISTINCT subscribers.*", :order => "subscribers.nick" has_one :essay, :primary_key => :name, :as => :writer -- cgit v1.2.3 From a34391c3b495bad268204bdf4f6b3483a61abcd5 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sat, 2 Oct 2010 21:45:46 +0100 Subject: Add support for table aliasing, with a test that needs aliasing in order to work correctly. This test incidentally provides a more complicated test case (4 inner joins, 2 using polymorphism). --- activerecord/test/models/author.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 1efb4fc095..1fbd729b60 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -84,8 +84,9 @@ class Author < ActiveRecord::Base has_many :favorite_authors, :through => :author_favorites, :order => 'name' has_many :tagging, :through => :posts # through polymorphic has_one - has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many TODO: Why is the :source needed? + has_many :taggings, :through => :posts # through polymorphic has_many has_many :tags, :through => :posts # through has_many :through (on source reflection + polymorphic) + has_many :similar_posts, :through => :tags, :source => :tagged_posts has_many :distinct_tags, :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name" has_many :post_categories, :through => :posts, :source => :categories -- cgit v1.2.3 From c37a5e7acde436b359043a67b7daace8be6f08c6 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 12 Oct 2010 18:16:31 +0100 Subject: Add a commented, failing test for using a habtm in a has many through association. I want to refactor how aliasing works first. --- activerecord/test/models/author.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 1fbd729b60..584164f19a 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -83,9 +83,9 @@ class Author < ActiveRecord::Base has_many :author_favorites has_many :favorite_authors, :through => :author_favorites, :order => 'name' - has_many :tagging, :through => :posts # through polymorphic has_one - has_many :taggings, :through => :posts # through polymorphic has_many - has_many :tags, :through => :posts # through has_many :through (on source reflection + polymorphic) + has_many :tagging, :through => :posts + has_many :taggings, :through => :posts + has_many :tags, :through => :posts has_many :similar_posts, :through => :tags, :source => :tagged_posts has_many :distinct_tags, :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name" has_many :post_categories, :through => :posts, :source => :categories @@ -100,6 +100,8 @@ class Author < ActiveRecord::Base belongs_to :author_address, :dependent => :destroy belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" + has_many :post_categories, :through => :posts, :source => :categories + scope :relation_include_posts, includes(:posts) scope :relation_include_tags, includes(:tags) -- cgit v1.2.3 From bc821a56114ae6f6d0b595475ad9e71f01f46f35 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 14 Oct 2010 12:59:16 +0100 Subject: Added test_has_many_through_has_many_with_has_many_through_habtm_source_reflection and make it pass --- activerecord/test/models/author.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 584164f19a..f2f373af8c 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -101,6 +101,7 @@ class Author < ActiveRecord::Base belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" has_many :post_categories, :through => :posts, :source => :categories + has_many :category_post_comments, :through => :categories, :source => :post_comments scope :relation_include_posts, includes(:posts) scope :relation_include_tags, includes(:tags) -- cgit v1.2.3 From 7963c30ebaeb511f7ddacc99ae2c7a530059ae6b Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 14 Oct 2010 13:07:28 +0100 Subject: Add test_has_many_through_has_many_through_with_belongs_to_source_reflection (which already works) --- activerecord/test/models/author.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index f2f373af8c..b5f702018a 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -89,6 +89,7 @@ class Author < ActiveRecord::Base has_many :similar_posts, :through => :tags, :source => :tagged_posts has_many :distinct_tags, :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name" has_many :post_categories, :through => :posts, :source => :categories + has_many :tagging_tags, :through => :taggings, :source => :tag has_many :books has_many :subscriptions, :through => :books -- cgit v1.2.3 From 9ec07348749675110843c44f680da79223218db2 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 19 Oct 2010 00:27:40 +0100 Subject: Properly support conditions on any of the reflections involved in a nested through association --- activerecord/test/models/author.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index b5f702018a..c0e082836d 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -86,7 +86,7 @@ class Author < ActiveRecord::Base has_many :tagging, :through => :posts has_many :taggings, :through => :posts has_many :tags, :through => :posts - has_many :similar_posts, :through => :tags, :source => :tagged_posts + has_many :similar_posts, :through => :tags, :source => :tagged_posts, :uniq => true has_many :distinct_tags, :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name" has_many :post_categories, :through => :posts, :source => :categories has_many :tagging_tags, :through => :taggings, :source => :tag @@ -103,6 +103,11 @@ class Author < ActiveRecord::Base has_many :post_categories, :through => :posts, :source => :categories has_many :category_post_comments, :through => :categories, :source => :post_comments + + has_many :misc_posts, :class_name => 'Post', :conditions => "posts.title LIKE 'misc post%'" + has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags + + has_many :misc_post_first_blue_tags_2, :through => :posts, :source => :first_blue_tags_2, :conditions => "posts.title LIKE 'misc post%'" scope :relation_include_posts, includes(:posts) scope :relation_include_tags, includes(:tags) -- cgit v1.2.3 From 596cc3b2329a9cc4a30c95c157ce36b2d08975df Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 19 Oct 2010 12:47:19 +0100 Subject: Respect the :primary_key option on the through_reflection of (non-nested) through associations --- activerecord/test/models/author.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index c0e082836d..1ba01d6b6b 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -95,8 +95,18 @@ class Author < ActiveRecord::Base has_many :subscriptions, :through => :books has_many :subscribers, :through => :subscriptions, :order => "subscribers.nick" # through has_many :through (on through reflection) has_many :distinct_subscribers, :through => :subscriptions, :source => :subscriber, :select => "DISTINCT subscribers.*", :order => "subscribers.nick" - + has_one :essay, :primary_key => :name, :as => :writer + has_one :essay_category, :through => :essay, :source => :category + + has_one :essay_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id + has_one :essay_category_2, :through => :essay_2, :source => :category + + has_many :essays, :primary_key => :name, :as => :writer + has_many :essay_categories, :through => :essays, :source => :category + + 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 :author_address, :dependent => :destroy belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress" -- cgit v1.2.3 From 01838636c6136d9a649ace71db61bb7990f9bd82 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 19 Oct 2010 14:14:06 +0100 Subject: Support for :primary_key option on the source reflection of a through association, where the source is a has_one or has_many --- activerecord/test/models/author.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 1ba01d6b6b..53b3b80950 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -98,12 +98,14 @@ class Author < ActiveRecord::Base has_one :essay, :primary_key => :name, :as => :writer has_one :essay_category, :through => :essay, :source => :category + has_one :essay_owner, :through => :essay, :source => :owner has_one :essay_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id has_one :essay_category_2, :through => :essay_2, :source => :category has_many :essays, :primary_key => :name, :as => :writer has_many :essay_categories, :through => :essays, :source => :category + has_many :essay_owners, :through => :essays, :source => :owner has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id has_many :essay_categories_2, :through => :essays_2, :source => :category -- cgit v1.2.3 From 915ea5ea826d48107e4c1953c7a32cf26727d10e Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 19 Oct 2010 16:13:06 +0100 Subject: Support the :primary_key option on a through reflection in a nested through association --- activerecord/test/models/author.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'activerecord/test/models/author.rb') 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" -- cgit v1.2.3 From 82b889f7d37249adaa606558d4c05356b3e84d9a Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Tue, 19 Oct 2010 17:22:42 +0100 Subject: Add explicit tests for the nested through association changes in reflection.rb --- activerecord/test/models/author.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index dd8a20ce9b..7dcfbd268b 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -90,6 +90,7 @@ class Author < ActiveRecord::Base has_many :distinct_tags, :through => :posts, :source => :tags, :select => "DISTINCT tags.*", :order => "tags.name" has_many :post_categories, :through => :posts, :source => :categories has_many :tagging_tags, :through => :taggings, :source => :tag + has_many :tags_with_primary_key, :through => :posts has_many :books has_many :subscriptions, :through => :books -- cgit v1.2.3 From 9a1a32ac2b8a526f543367bc7e8258bbd7e6a164 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 31 Oct 2010 11:21:28 +0000 Subject: Fix naughty trailing whitespace --- activerecord/test/models/author.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 7dcfbd268b..43bfd93e60 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -96,7 +96,7 @@ class Author < ActiveRecord::Base has_many :subscriptions, :through => :books has_many :subscribers, :through => :subscriptions, :order => "subscribers.nick" # through has_many :through (on through reflection) has_many :distinct_subscribers, :through => :subscriptions, :source => :subscriber, :select => "DISTINCT subscribers.*", :order => "subscribers.nick" - + has_one :essay, :primary_key => :name, :as => :writer has_one :essay_category, :through => :essay, :source => :category has_one :essay_owner, :through => :essay, :source => :owner @@ -107,7 +107,7 @@ class Author < ActiveRecord::Base has_many :essays, :primary_key => :name, :as => :writer has_many :essay_categories, :through => :essays, :source => :category has_many :essay_owners, :through => :essays, :source => :owner - + has_many :essays_2, :primary_key => :name, :class_name => 'Essay', :foreign_key => :author_id has_many :essay_categories_2, :through => :essays_2, :source => :category @@ -119,7 +119,7 @@ class Author < ActiveRecord::Base has_many :post_categories, :through => :posts, :source => :categories has_many :category_post_comments, :through => :categories, :source => :post_comments - + has_many :misc_posts, :class_name => 'Post', :conditions => "posts.title LIKE 'misc post%'" has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags -- cgit v1.2.3 From 9abc94c44516afdcfe4a3b202c336c9578fd6d0d Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Wed, 16 Mar 2011 18:54:34 +0000 Subject: oracle, y u defy me --- activerecord/test/models/author.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activerecord/test/models/author.rb') diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index f362f70dec..e0cbc44265 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -131,10 +131,12 @@ class Author < ActiveRecord::Base has_many :post_categories, :through => :posts, :source => :categories has_many :category_post_comments, :through => :categories, :source => :post_comments - has_many :misc_posts, :class_name => 'Post', :conditions => "posts.title LIKE 'misc post%'" + has_many :misc_posts, :class_name => 'Post', + :conditions => { :posts => { :title => ['misc post by bob', 'misc post by mary'] } } has_many :misc_post_first_blue_tags, :through => :misc_posts, :source => :first_blue_tags - has_many :misc_post_first_blue_tags_2, :through => :posts, :source => :first_blue_tags_2, :conditions => "posts.title LIKE 'misc post%'" + has_many :misc_post_first_blue_tags_2, :through => :posts, :source => :first_blue_tags_2, + :conditions => { :posts => { :title => ['misc post by bob', 'misc post by mary'] } } scope :relation_include_posts, includes(:posts) scope :relation_include_tags, includes(:tags) -- cgit v1.2.3