Skip to content
This repository
Browse code

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
  • Loading branch information...
commit fdd268138a062b45aa126c21ee3d5579f01c3f2e 1 parent 2119e06
David Heinemeier Hansson authored
8  activerecord/lib/active_record/base.rb
@@ -732,9 +732,11 @@ def add_conditions!(sql, conditions)
732 732
         end
733 733
         
734 734
         def type_condition
735  
-          " (" + subclasses.inject("#{inheritance_column} = '#{Inflector.demodulize(name)}' ") do |condition, subclass| 
736  
-            condition << "OR #{inheritance_column} = '#{Inflector.demodulize(subclass.name)}' "
737  
-          end + ") "
  735
+          type_condition = subclasses.inject("#{table_name}.#{inheritance_column} = '#{name.demodulize}' ") do |condition, subclass| 
  736
+            condition << "OR #{table_name}.#{inheritance_column} = '#{subclass.name.demodulize}' "
  737
+          end
  738
+          
  739
+          return " (#{type_condition})"
738 740
         end
739 741
 
740 742
         # Guesses the table name, but does not decorate it with prefix and suffix information.
5  activerecord/test/associations_go_eager_test.rb
@@ -32,5 +32,10 @@ def test_eager_association_loading_with_belongs_to
32 32
     assert_equal @welcome.title, comments.first.post.title
33 33
     assert_equal @thinking.title, comments.last.post.title
34 34
   end
  35
+  
  36
+  def test_eager_with_inheritance
  37
+    posts = SpecialPost.find(:all, :include => [ :comments ])
  38
+  end
  39
+  
35 40
 end
36 41
 
4  activerecord/test/fixtures/comment.rb
... ...
@@ -1,3 +1,5 @@
1 1
 class Comment < ActiveRecord::Base
2 2
   belongs_to :post
3  
-end
  3
+end
  4
+
  5
+class SpecialComment < Comment; end;
7  activerecord/test/fixtures/comments.yml
@@ -2,13 +2,16 @@ greetings:
2 2
   id: 1
3 3
   post_id: 1
4 4
   body: Thank you for the welcome
  5
+  type: Comment
5 6
 
6 7
 more_greetings:
7 8
   id: 2
8 9
   post_id: 1
9 10
   body: Thank you again for the welcome
10  
-
  11
+  type: Comment
  12
+  
11 13
 does_it_hurt:
12 14
   id: 3
13 15
   post_id: 2
14  
-  body: Don't think too hard
  16
+  body: Don't think too hard
  17
+  type: SpecialComment
3  activerecord/test/fixtures/db_definitions/db2.drop.sql
@@ -15,5 +15,8 @@ DROP TABLE mixins;
15 15
 DROP TABLE people;
16 16
 DROP TABLE binaries;
17 17
 DROP TABLE computers;
  18
+DROP TABLE posts;
  19
+DROP TABLE comments;
  20
+DROP TABLE authors;
18 21
 DROP TABLE tasks;
19 22
 
20  activerecord/test/fixtures/db_definitions/db2.sql
@@ -129,6 +129,26 @@ CREATE TABLE computers (
129 129
   developer int NOT NULL
130 130
 );
131 131
 
  132
+CREATE TABLE posts (
  133
+  id int generated by default as identity (start with +10000),
  134
+  author_id int default NULL,
  135
+  title varchar(255) default NULL,
  136
+  type varchar(255) default NULL,
  137
+  body varchar(3000) default NULL
  138
+);
  139
+
  140
+CREATE TABLE comments (
  141
+  id int generated by default as identity (start with +10000),
  142
+  post_id int default NULL,
  143
+  type varchar(255) default NULL,
  144
+  body varchar(3000) default NULL
  145
+);
  146
+
  147
+CREATE TABLE authors (
  148
+  id int generated by default as identity (start with +10000),
  149
+  name varchar(255) default NULL
  150
+);
  151
+
132 152
 CREATE TABLE tasks (
133 153
   id int generated by default as identity (start with +10000),
134 154
   starting timestamp default NULL,
6  activerecord/test/fixtures/db_definitions/mysql.sql
@@ -133,13 +133,15 @@ CREATE TABLE `posts` (
133 133
   `id` INTEGER NOT NULL PRIMARY KEY,
134 134
   `author_id` INTEGER NOT NULL,
135 135
   `title` VARCHAR(255) NOT NULL,
136  
-  `body` TEXT NOT NULL
  136
+  `body` TEXT NOT NULL,
  137
+  `type` VARCHAR(255) NOT NULL
137 138
 ) TYPE=InnoDB;
138 139
 
139 140
 CREATE TABLE `comments` (
140 141
   `id` INTEGER NOT NULL PRIMARY KEY,
141 142
   `post_id` INTEGER NOT NULL,
142  
-  `body` TEXT NOT NULL
  143
+  `body` TEXT NOT NULL,
  144
+  `type` VARCHAR(255) NOT NULL
143 145
 ) TYPE=InnoDB;
144 146
 
145 147
 CREATE TABLE `authors` (
3  activerecord/test/fixtures/db_definitions/oci.drop.sql
@@ -14,5 +14,8 @@ drop table colnametests;
14 14
 drop table mixins;
15 15
 drop table people;
16 16
 drop table binaries;
  17
+drop table posts;
  18
+drop table comments;
  19
+drop table authors;
17 20
 drop table computers;
18 21
 drop sequence rails_sequence;
20  activerecord/test/fixtures/db_definitions/oci.sql
@@ -167,6 +167,26 @@ create table computers (
167 167
   developer integer not null references developers initially deferred disable
168 168
 );
169 169
 
  170
+create table posts (
  171
+  id integer not null primary key,
  172
+  author_id integer default null,
  173
+  title varchar(255) default null,
  174
+  type varchar(255) default null,
  175
+  body varchar(3000) default null
  176
+);
  177
+
  178
+create table comments (
  179
+  id integer not null primary key,
  180
+  post_id integer default null,
  181
+  type varchar(255) default null,
  182
+  body varchar(3000) default null
  183
+);
  184
+
  185
+create table authors (
  186
+  id integer not null primary key,
  187
+  name varchar(255) default null
  188
+);
  189
+
170 190
 create table tasks (
171 191
   id integer not null primary key,
172 192
   starting date default null,
3  activerecord/test/fixtures/db_definitions/postgresql.drop.sql
@@ -15,5 +15,8 @@ DROP TABLE mixins;
15 15
 DROP TABLE people;
16 16
 DROP TABLE binaries;
17 17
 DROP TABLE computers;
  18
+DROP TABLE posts;
  19
+DROP TABLE comments;
  20
+DROP TABLE authors;
18 21
 DROP TABLE tasks;
19 22
 
20  activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -147,6 +147,26 @@ CREATE TABLE computers (
147 147
   developer integer NOT NULL
148 148
 );
149 149
 
  150
+CREATE TABLE posts (
  151
+  id serial,
  152
+  author_id integer,
  153
+  title varchar(255),
  154
+  type varchar(255),
  155
+  body text
  156
+);
  157
+
  158
+CREATE TABLE comments (
  159
+  id serial,
  160
+  post_id integer,
  161
+  type varchar(255),
  162
+  body text
  163
+);
  164
+
  165
+CREATE TABLE authors (
  166
+  id serial,
  167
+  name varchar(255) default NULL
  168
+);
  169
+
150 170
 CREATE TABLE taske (
151 171
   id serial,
152 172
   starting timestamp,
2  activerecord/test/fixtures/db_definitions/sqlite.sql
@@ -120,12 +120,14 @@ CREATE TABLE 'posts' (
120 120
   'id' INTEGER NOT NULL PRIMARY KEY,
121 121
   'author_id' INTEGER NOT NULL,
122 122
   'title' VARCHAR(255) NOT NULL,
  123
+  'type' VARCHAR(255) NOT NULL,
123 124
   'body' TEXT NOT NULL
124 125
 );
125 126
 
126 127
 CREATE TABLE 'comments' (
127 128
   'id' INTEGER NOT NULL PRIMARY KEY,
128 129
   'post_id' INTEGER NOT NULL,
  130
+  'type' VARCHAR(255) NOT NULL,
129 131
   'body' TEXT NOT NULL
130 132
 );
131 133
 
3  activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
@@ -15,4 +15,7 @@ DROP TABLE mixins;
15 15
 DROP TABLE people;
16 16
 DROP TABLE binaries;
17 17
 DROP TABLE computers;
  18
+DROP TABLE posts;
  19
+DROP TABLE comments;
  20
+DROP TABLE authors;
18 21
 DROP TABLE tasks;
20  activerecord/test/fixtures/db_definitions/sqlserver.sql
@@ -116,6 +116,26 @@ CREATE TABLE computers (
116 116
   developer int NOT NULL
117 117
 );
118 118
 
  119
+CREATE TABLE posts (
  120
+  id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
  121
+  author_id int default NULL,
  122
+  title varchar(255) default NULL,
  123
+  type varchar(255) default NULL,
  124
+  body text default NULL
  125
+);
  126
+
  127
+CREATE TABLE comments (
  128
+  id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
  129
+  post_id int default NULL,
  130
+  type varchar(255) default NULL,
  131
+  body text default NULL
  132
+);
  133
+
  134
+CREATE TABLE authors (
  135
+  id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
  136
+  name varchar(255) default NULL
  137
+);
  138
+
119 139
 CREATE TABLE tasks (
120 140
   id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
121 141
   starting datetime default NULL,
4  activerecord/test/fixtures/post.rb
... ...
@@ -1,4 +1,6 @@
1 1
 class Post < ActiveRecord::Base
2 2
   belongs_to :author
3 3
   has_many   :comments
4  
-end
  4
+end
  5
+
  6
+class SpecialPost < Post; end
2  activerecord/test/fixtures/posts.yml
@@ -3,9 +3,11 @@ welcome:
3 3
   author_id: 1
4 4
   title: Welcome to the weblog
5 5
   body: Such a lovely day
  6
+  type: Post
6 7
 
7 8
 thinking:
8 9
   id: 2
9 10
   author_id: 1
10 11
   title: So I was thinking
11 12
   body: Like I hopefully always am
  13
+  type: SpecialPost

0 notes on commit fdd2681

Please sign in to comment.
Something went wrong with that request. Please try again.