Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@dhh dhh authored
View
8 activerecord/lib/active_record/base.rb
@@ -732,9 +732,11 @@ def add_conditions!(sql, conditions)
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.
View
5 activerecord/test/associations_go_eager_test.rb
@@ -32,5 +32,10 @@ def test_eager_association_loading_with_belongs_to
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
View
4 activerecord/test/fixtures/comment.rb
@@ -1,3 +1,5 @@
class Comment < ActiveRecord::Base
belongs_to :post
-end
+end
+
+class SpecialComment < Comment; end;
View
7 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
+ body: Don't think too hard
+ type: SpecialComment
View
3  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;
View
20 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,
View
6 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` (
View
3  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;
View
20 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,
View
3  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;
View
20 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,
View
2  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
);
View
3  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;
View
20 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,
View
4 activerecord/test/fixtures/post.rb
@@ -1,4 +1,6 @@
class Post < ActiveRecord::Base
belongs_to :author
has_many :comments
-end
+end
+
+class SpecialPost < Post; end
View
2  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
Please sign in to comment.
Something went wrong with that request. Please try again.