From 5f4b66201eb9bd01be96a212adfea8fb95334380 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Fri, 10 Feb 2006 05:19:41 +0000 Subject: Allow has_many :through to work with :include [Michael Schoen]. Closes #3611 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3566 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_go_eager_test.rb | 13 ++++++++++++- activerecord/test/fixtures/db_definitions/db2.sql | 7 +++++++ activerecord/test/fixtures/db_definitions/firebird.sql | 9 +++++++++ activerecord/test/fixtures/db_definitions/mysql.sql | 6 ++++++ activerecord/test/fixtures/db_definitions/oci.sql | 8 ++++++++ activerecord/test/fixtures/db_definitions/postgresql.sql | 7 +++++++ activerecord/test/fixtures/db_definitions/sqlite.sql | 6 ++++++ activerecord/test/fixtures/db_definitions/sqlserver.sql | 7 +++++++ activerecord/test/fixtures/person.rb | 5 ++++- activerecord/test/fixtures/post.rb | 3 +++ 10 files changed, 69 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb index a03c201d76..1bc9d9f982 100644 --- a/activerecord/test/associations_go_eager_test.rb +++ b/activerecord/test/associations_go_eager_test.rb @@ -4,10 +4,12 @@ require 'fixtures/comment' require 'fixtures/author' require 'fixtures/category' require 'fixtures/company' +require 'fixtures/person' +require 'fixtures/reader' class EagerAssociationTest < Test::Unit::TestCase fixtures :posts, :comments, :authors, :categories, :categories_posts, - :companies, :accounts + :companies, :accounts, :tags, :people, :readers def test_loading_with_one_association posts = Post.find(:all, :include => :comments) @@ -101,6 +103,15 @@ class EagerAssociationTest < Test::Unit::TestCase assert_equal [], posts end + def test_eager_with_has_many_through + posts_with_comments = people(:michael).posts.find(:all, :include => :comments ) + posts_with_author = people(:michael).posts.find(:all, :include => :author ) + posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ]) + assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size } + assert_equal authors(:david), posts_with_author.first.author + assert_equal authors(:david), posts_with_comments_and_author.first.author + end + def test_eager_with_has_many_and_limit posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2) assert_equal 2, posts.size diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 9bb2ad991a..d570f925e9 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/activerecord/test/fixtures/db_definitions/db2.sql @@ -130,6 +130,13 @@ CREATE TABLE people ( PRIMARY KEY (id) ); +CREATE TABLE readers ( + id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000), + post_id INT NOT NULL, + person_id INT NOT NULL, + PRIMARY KEY (id) +); + CREATE TABLE binaries ( id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000), data BLOB(50000), diff --git a/activerecord/test/fixtures/db_definitions/firebird.sql b/activerecord/test/fixtures/db_definitions/firebird.sql index 28e4c71b71..87eb00c53f 100644 --- a/activerecord/test/fixtures/db_definitions/firebird.sql +++ b/activerecord/test/fixtures/db_definitions/firebird.sql @@ -161,6 +161,15 @@ CREATE TABLE people ( CREATE GENERATOR people_seq; SET GENERATOR people_seq TO 10000; +CREATE TABLE readers ( + id BIGINT NOT NULL, + post_id BIGINT NOT NULL, + person_id BIGINT NOT NULL, + PRIMARY KEY (id) +); +CREATE GENERATOR readers_seq; +SET GENERATOR readers_seq TO 10000; + CREATE TABLE binaries ( id BIGINT NOT NULL, data BLOB, diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 83479393f4..21c3b0176e 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -130,6 +130,12 @@ CREATE TABLE `people` ( `lock_version` INTEGER NOT NULL DEFAULT 0 ) TYPE=InnoDB; +CREATE TABLE `readers` ( + `id` int(11) NOT NULL PRIMARY KEY, + `post_id` INTEGER NOT NULL, + `person_id` INTEGER NOT NULL +) TYPE=InnoDB; + CREATE TABLE `binaries` ( `id` int(11) NOT NULL auto_increment, `data` mediumblob, diff --git a/activerecord/test/fixtures/db_definitions/oci.sql b/activerecord/test/fixtures/db_definitions/oci.sql index 9995f4c85e..760659d1c4 100644 --- a/activerecord/test/fixtures/db_definitions/oci.sql +++ b/activerecord/test/fixtures/db_definitions/oci.sql @@ -187,6 +187,14 @@ create table people ( ); create sequence people_seq minvalue 10000; +create table readers ( + id integer not null, + post_id integer not null, + person_id integer not null, + primary key (id) +); +create sequence readers_seq minvalue 10000; + create table binaries ( id integer not null, data blob null, diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index c01afc206d..490f915791 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -153,6 +153,13 @@ CREATE TABLE people ( PRIMARY KEY (id) ); +CREATE TABLE readers ( + id serial, + post_id integer NOT NULL, + person_id integer NOT NULL, + primary key (id) +); + CREATE TABLE binaries ( id serial , data bytea, diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index b66969a40b..685c2abc8b 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -118,6 +118,12 @@ CREATE TABLE 'people' ( 'lock_version' INTEGER NOT NULL DEFAULT 0 ); +CREATE TABLE 'readers' ( + 'id' INTEGER NOT NULL PRIMARY KEY, + 'post_id' INTEGER NOT NULL, + 'person_id' INTEGER NOT NULL +); + CREATE TABLE 'binaries' ( 'id' INTEGER NOT NULL PRIMARY KEY, 'data' BLOB DEFAULT NULL diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index 81399b7d26..5999bdd1b4 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -118,6 +118,13 @@ CREATE TABLE people ( PRIMARY KEY (id) ); +CREATE TABLE readers ( + id int NOT NULL IDENTITY(1, 1), + post_id int NOT NULL, + person_id int NOT NULL, + primary key (id) +); + CREATE TABLE binaries ( id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, data image NULL diff --git a/activerecord/test/fixtures/person.rb b/activerecord/test/fixtures/person.rb index 4fa5811c71..7a9666f4eb 100644 --- a/activerecord/test/fixtures/person.rb +++ b/activerecord/test/fixtures/person.rb @@ -1 +1,4 @@ -class Person < ActiveRecord::Base; end +class Person < ActiveRecord::Base + has_many :readers + has_many :posts, :through => :readers +end diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb index 4c2ccb3f43..7c1f8e1961 100644 --- a/activerecord/test/fixtures/post.rb +++ b/activerecord/test/fixtures/post.rb @@ -28,6 +28,9 @@ class Post < ActiveRecord::Base has_many :categorizations, :foreign_key => :category_id has_many :authors, :through => :categorizations + has_many :readers + has_many :people, :through => :readers + def self.what_are_you 'a post...' end -- cgit v1.2.3