提交 5f4b6620 编写于 作者: M Michael Koziarski

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
上级 7e6d5b51
*SVN*
* Allow :include to be used with has_many :through associations #3611 [Michael Schoen]
* PostgreSQL: smarter schema dumps using pk_and_sequence_for(table). #2920 [Blair Zajac]
* SQLServer: more compatible limit/offset emulation. #3779 [Tom Ward]
......
......@@ -1028,7 +1028,7 @@ def construct_counter_sql_with_included_associations(options, reflections)
end
def construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections)
sql = "SELECT #{column_aliases(schema_abbreviations)} FROM #{table_name} "
sql = "SELECT #{column_aliases(schema_abbreviations)} FROM #{options[:from] || table_name} "
sql << reflections.collect { |reflection| association_join(reflection) }.to_s
sql << "#{options[:joins]} " if options[:joins]
......
......@@ -80,7 +80,7 @@ def construct_conditions
end
def construct_from
"#{@reflection.table_name}, #{@owner.class.reflections[@reflection.options[:through]].table_name}"
"#{@owner.class.reflections[@reflection.options[:through]].table_name}, #{@reflection.table_name}"
end
def construct_select
......
......@@ -4,10 +4,12 @@
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 @@ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_mult
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
......
......@@ -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),
......
......@@ -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,
......
......@@ -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,
......
......@@ -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,
......
......@@ -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,
......
......@@ -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
......
......@@ -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
......
class Person < ActiveRecord::Base; end
class Person < ActiveRecord::Base
has_many :readers
has_many :posts, :through => :readers
end
......@@ -28,6 +28,9 @@ def find_most_recent
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册