diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-10 15:49:49 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-10 15:49:49 +0000 |
commit | fdd268138a062b45aa126c21ee3d5579f01c3f2e (patch) | |
tree | 28c593bd479bb8be02e4b028a8c0e1bc4fe638dd /activerecord | |
parent | 2119e0699a182575349b1c3a311b182e171e097c (diff) | |
download | rails-fdd268138a062b45aa126c21ee3d5579f01c3f2e.tar.gz rails-fdd268138a062b45aa126c21ee3d5579f01c3f2e.tar.bz2 rails-fdd268138a062b45aa126c21ee3d5579f01c3f2e.zip |
Made eager loading work with inheritance hierarchies #1065 [Ryan Carver]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1131 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 8 | ||||
-rw-r--r-- | activerecord/test/associations_go_eager_test.rb | 5 | ||||
-rw-r--r-- | activerecord/test/fixtures/comment.rb | 4 | ||||
-rw-r--r-- | activerecord/test/fixtures/comments.yml | 7 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/db2.drop.sql | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/db2.sql | 20 | ||||
-rwxr-xr-x | activerecord/test/fixtures/db_definitions/mysql.sql | 6 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/oci.drop.sql | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/oci.sql | 20 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/postgresql.drop.sql | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/postgresql.sql | 20 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/sqlite.sql | 2 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/sqlserver.drop.sql | 3 | ||||
-rw-r--r-- | activerecord/test/fixtures/db_definitions/sqlserver.sql | 20 | ||||
-rw-r--r-- | activerecord/test/fixtures/post.rb | 4 | ||||
-rw-r--r-- | activerecord/test/fixtures/posts.yml | 2 |
16 files changed, 121 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c27bcecdd6..3a1a590be8 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -732,9 +732,11 @@ module ActiveRecord #:nodoc: end def type_condition - " (" + subclasses.inject("#{inheritance_column} = '#{Inflector.demodulize(name)}' ") do |condition, subclass| - condition << "OR #{inheritance_column} = '#{Inflector.demodulize(subclass.name)}' " - end + ") " + type_condition = subclasses.inject("#{table_name}.#{inheritance_column} = '#{name.demodulize}' ") do |condition, subclass| + condition << "OR #{table_name}.#{inheritance_column} = '#{subclass.name.demodulize}' " + end + + return " (#{type_condition})" end # Guesses the table name, but does not decorate it with prefix and suffix information. diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb index 59ca4cac85..a8e99c1bc6 100644 --- a/activerecord/test/associations_go_eager_test.rb +++ b/activerecord/test/associations_go_eager_test.rb @@ -32,5 +32,10 @@ class EagerAssociationTest < Test::Unit::TestCase assert_equal @welcome.title, comments.first.post.title assert_equal @thinking.title, comments.last.post.title end + + def test_eager_with_inheritance + posts = SpecialPost.find(:all, :include => [ :comments ]) + end + end diff --git a/activerecord/test/fixtures/comment.rb b/activerecord/test/fixtures/comment.rb index 662b7db66b..b59819864d 100644 --- a/activerecord/test/fixtures/comment.rb +++ b/activerecord/test/fixtures/comment.rb @@ -1,3 +1,5 @@ class Comment < ActiveRecord::Base belongs_to :post -end
\ No newline at end of file +end + +class SpecialComment < Comment; end;
\ No newline at end of file diff --git a/activerecord/test/fixtures/comments.yml b/activerecord/test/fixtures/comments.yml index f970806b0b..9c442cf794 100644 --- a/activerecord/test/fixtures/comments.yml +++ b/activerecord/test/fixtures/comments.yml @@ -2,13 +2,16 @@ greetings: id: 1 post_id: 1 body: Thank you for the welcome + type: Comment more_greetings: id: 2 post_id: 1 body: Thank you again for the welcome - + type: Comment + does_it_hurt: id: 3 post_id: 2 - body: Don't think too hard
\ No newline at end of file + body: Don't think too hard + type: SpecialComment
\ No newline at end of file diff --git a/activerecord/test/fixtures/db_definitions/db2.drop.sql b/activerecord/test/fixtures/db_definitions/db2.drop.sql index 88e6516c81..e718d4b321 100644 --- a/activerecord/test/fixtures/db_definitions/db2.drop.sql +++ b/activerecord/test/fixtures/db_definitions/db2.drop.sql @@ -15,5 +15,8 @@ DROP TABLE mixins; DROP TABLE people; DROP TABLE binaries; DROP TABLE computers; +DROP TABLE posts; +DROP TABLE comments; +DROP TABLE authors; DROP TABLE tasks; diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 856847e500..a24318bf4b 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/activerecord/test/fixtures/db_definitions/db2.sql @@ -129,6 +129,26 @@ CREATE TABLE computers ( developer int NOT NULL ); +CREATE TABLE posts ( + id int generated by default as identity (start with +10000), + author_id int default NULL, + title varchar(255) default NULL, + type varchar(255) default NULL, + body varchar(3000) default NULL +); + +CREATE TABLE comments ( + id int generated by default as identity (start with +10000), + post_id int default NULL, + type varchar(255) default NULL, + body varchar(3000) default NULL +); + +CREATE TABLE authors ( + id int generated by default as identity (start with +10000), + name varchar(255) default NULL +); + CREATE TABLE tasks ( id int generated by default as identity (start with +10000), starting timestamp default NULL, diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 235a1c84d1..17d443bffd 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -133,13 +133,15 @@ CREATE TABLE `posts` ( `id` INTEGER NOT NULL PRIMARY KEY, `author_id` INTEGER NOT NULL, `title` VARCHAR(255) NOT NULL, - `body` TEXT NOT NULL + `body` TEXT NOT NULL, + `type` VARCHAR(255) NOT NULL ) TYPE=InnoDB; CREATE TABLE `comments` ( `id` INTEGER NOT NULL PRIMARY KEY, `post_id` INTEGER NOT NULL, - `body` TEXT NOT NULL + `body` TEXT NOT NULL, + `type` VARCHAR(255) NOT NULL ) TYPE=InnoDB; CREATE TABLE `authors` ( diff --git a/activerecord/test/fixtures/db_definitions/oci.drop.sql b/activerecord/test/fixtures/db_definitions/oci.drop.sql index 255e024531..c98a8790c9 100644 --- a/activerecord/test/fixtures/db_definitions/oci.drop.sql +++ b/activerecord/test/fixtures/db_definitions/oci.drop.sql @@ -14,5 +14,8 @@ drop table colnametests; drop table mixins; drop table people; drop table binaries; +drop table posts; +drop table comments; +drop table authors; drop table computers; drop sequence rails_sequence; diff --git a/activerecord/test/fixtures/db_definitions/oci.sql b/activerecord/test/fixtures/db_definitions/oci.sql index b17b1091cb..bb4726caaa 100644 --- a/activerecord/test/fixtures/db_definitions/oci.sql +++ b/activerecord/test/fixtures/db_definitions/oci.sql @@ -167,6 +167,26 @@ create table computers ( developer integer not null references developers initially deferred disable ); +create table posts ( + id integer not null primary key, + author_id integer default null, + title varchar(255) default null, + type varchar(255) default null, + body varchar(3000) default null +); + +create table comments ( + id integer not null primary key, + post_id integer default null, + type varchar(255) default null, + body varchar(3000) default null +); + +create table authors ( + id integer not null primary key, + name varchar(255) default null +); + create table tasks ( id integer not null primary key, starting date default null, diff --git a/activerecord/test/fixtures/db_definitions/postgresql.drop.sql b/activerecord/test/fixtures/db_definitions/postgresql.drop.sql index 88e6516c81..e718d4b321 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.drop.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.drop.sql @@ -15,5 +15,8 @@ DROP TABLE mixins; DROP TABLE people; DROP TABLE binaries; DROP TABLE computers; +DROP TABLE posts; +DROP TABLE comments; +DROP TABLE authors; DROP TABLE tasks; diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 53618b653d..01d6ee288c 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -147,6 +147,26 @@ CREATE TABLE computers ( developer integer NOT NULL ); +CREATE TABLE posts ( + id serial, + author_id integer, + title varchar(255), + type varchar(255), + body text +); + +CREATE TABLE comments ( + id serial, + post_id integer, + type varchar(255), + body text +); + +CREATE TABLE authors ( + id serial, + name varchar(255) default NULL +); + CREATE TABLE taske ( id serial, starting timestamp, diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index 8639182266..0d05ca6932 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -120,12 +120,14 @@ CREATE TABLE 'posts' ( 'id' INTEGER NOT NULL PRIMARY KEY, 'author_id' INTEGER NOT NULL, 'title' VARCHAR(255) NOT NULL, + 'type' VARCHAR(255) NOT NULL, 'body' TEXT NOT NULL ); CREATE TABLE 'comments' ( 'id' INTEGER NOT NULL PRIMARY KEY, 'post_id' INTEGER NOT NULL, + 'type' VARCHAR(255) NOT NULL, 'body' TEXT NOT NULL ); diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql index 480b2703c4..2ec7dbf026 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql @@ -15,4 +15,7 @@ DROP TABLE mixins; DROP TABLE people; DROP TABLE binaries; DROP TABLE computers; +DROP TABLE posts; +DROP TABLE comments; +DROP TABLE authors; DROP TABLE tasks; diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index de746efb57..8e0d78bb61 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -116,6 +116,26 @@ CREATE TABLE computers ( developer int NOT NULL ); +CREATE TABLE posts ( + id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, + author_id int default NULL, + title varchar(255) default NULL, + type varchar(255) default NULL, + body text default NULL +); + +CREATE TABLE comments ( + id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, + post_id int default NULL, + type varchar(255) default NULL, + body text default NULL +); + +CREATE TABLE authors ( + id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, + name varchar(255) default NULL +); + CREATE TABLE tasks ( id int NOT NULL IDENTITY(1, 1) PRIMARY KEY, starting datetime default NULL, diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb index a09e192c04..3c0ef927dc 100644 --- a/activerecord/test/fixtures/post.rb +++ b/activerecord/test/fixtures/post.rb @@ -1,4 +1,6 @@ class Post < ActiveRecord::Base belongs_to :author has_many :comments -end
\ No newline at end of file +end + +class SpecialPost < Post; end
\ No newline at end of file diff --git a/activerecord/test/fixtures/posts.yml b/activerecord/test/fixtures/posts.yml index 7c706b6818..f5effb20fc 100644 --- a/activerecord/test/fixtures/posts.yml +++ b/activerecord/test/fixtures/posts.yml @@ -3,9 +3,11 @@ welcome: author_id: 1 title: Welcome to the weblog body: Such a lovely day + type: Post thinking: id: 2 author_id: 1 title: So I was thinking body: Like I hopefully always am + type: SpecialPost |