diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-18 05:55:20 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-04-18 05:55:20 +0000 |
commit | 1a6954af01680d18135ce9a50ff273fd81dc6245 (patch) | |
tree | 2e2a6cf9892d5f5f785a62859ad2875ef1eefcc0 /activerecord | |
parent | bceb88ef9c402a722caa7441a53f768aa14ab709 (diff) | |
download | rails-1a6954af01680d18135ce9a50ff273fd81dc6245.tar.gz rails-1a6954af01680d18135ce9a50ff273fd81dc6245.tar.bz2 rails-1a6954af01680d18135ce9a50ff273fd81dc6245.zip |
Changed habtm eager loading to also use joins
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1201 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 36 | ||||
-rw-r--r-- | activerecord/test/associations_go_eager_test.rb | 15 | ||||
-rw-r--r-- | activerecord/test/fixtures/posts.yml | 1 |
4 files changed, 21 insertions, 33 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 108de3a8c8..abcd70cfc6 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed loading of fixtures in to be in the right order (or PostgreSQL would bark) #1047 [stephenh@chase3000.com] + * Fixed page caching for non-vhost applications living underneath the root #1004 [Ben Schumacher] * Fixes a problem with the SQL Adapter which was resulting in IDENTITY_INSERT not being set to ON when it should be #1104 [adelle] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index b1daf3df1e..fffd695294 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -708,16 +708,10 @@ module ActiveRecord def construct_finder_sql_with_included_associations(options, schema_abbreviations, reflections) habtm_associations = reflections.find_all { |r| r.macro == :has_and_belongs_to_many } - sql = "SELECT #{column_aliases(schema_abbreviations)} FROM #{table_name}" - add_habtm_join_tables!(habtm_associations, sql) - sql << " " - + sql = "SELECT #{column_aliases(schema_abbreviations)} FROM #{table_name} " add_association_joins!(reflections, sql) sql << "#{options[:joins]} " if options[:joins] - - add_habtm_conditions!(habtm_associations, options) add_conditions!(sql, options[:conditions]) - sql << "ORDER BY #{options[:order]} " if options[:order] return sanitize_sql(sql) @@ -727,35 +721,25 @@ module ActiveRecord schema_abbreviations.collect { |cn, tc| "#{tc.join(".")} AS #{cn}" }.join(", ") end - def add_habtm_join_tables!(habtm_associations, sql) - return if habtm_associations.empty? - sql << ", " + habtm_associations.collect { |a| [ a.klass.table_name, a.options[:join_table] ] }.join(", ") - end - - def add_habtm_conditions!(habtm_associations, options) - return if habtm_associations.empty? - options[:conditions] = [ - options[:conditions], - habtm_associations.collect { |r| - join_table = r.options[:join_table] - "#{join_table}.#{table_name.classify.foreign_key} = #{table_name}.#{primary_key} AND " + - "#{join_table}.#{r.klass.table_name.classify.foreign_key} = #{r.klass.table_name}.#{r.klass.primary_key}" - } - ].compact.join(" AND ") - end - def add_association_joins!(reflections, sql) reflections.each { |reflection| sql << association_join(reflection) } end def association_join(reflection) case reflection.macro + when :has_and_belongs_to_many + " LEFT OUTER JOIN #{reflection.options[:join_table]} ON " + + "#{reflection.options[:join_table]}.#{reflection.options[:foreign_key] || table_name.classify.foreign_key} = " + + "#{table_name}.#{primary_key} " + + " LEFT OUTER JOIN #{reflection.klass.table_name} ON " + + "#{reflection.options[:join_table]}.#{reflection.options[:associated_foreign_key] || reflection.klass.table_name.classify.foreign_key} = " + + "#{reflection.klass.table_name}.#{reflection.klass.primary_key} " when :has_many, :has_one - " LEFT JOIN #{reflection.klass.table_name} ON " + + " LEFT OUTER JOIN #{reflection.klass.table_name} ON " + "#{reflection.klass.table_name}.#{reflection.options[:foreign_key] || table_name.classify.foreign_key} = " + "#{table_name}.#{primary_key} " when :belongs_to - " LEFT JOIN #{reflection.klass.table_name} ON " + + " LEFT OUTER JOIN #{reflection.klass.table_name} ON " + "#{reflection.klass.table_name}.#{reflection.klass.primary_key} = " + "#{table_name}.#{reflection.options[:foreign_key] || reflection.klass.table_name.classify.foreign_key} " else diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb index 0bd887478d..8de37f3d92 100644 --- a/activerecord/test/associations_go_eager_test.rb +++ b/activerecord/test/associations_go_eager_test.rb @@ -18,10 +18,10 @@ class EagerAssociationTest < Test::Unit::TestCase end def test_loading_with_multiple_associations - posts = Post.find(:all, :include => [ :comments, :author, :categories ]) + posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id") assert_equal 2, posts.first.comments.size assert_equal 2, posts.first.categories.size - assert_equal @greetings.body, posts.first.comments.first.body + assert posts.first.comments.include?(@greetings) end def test_loading_from_an_association @@ -40,11 +40,12 @@ class EagerAssociationTest < Test::Unit::TestCase end def test_eager_association_loading_with_habtm - posts = Post.find(:all, :include => :categories) - assert_equal 2, posts.first.categories.size - assert_equal 1, posts.last.categories.size - assert_equal @technology.name, posts.first.categories.last.name - assert_equal @general.name, posts.last.categories.first.name + posts = Post.find(:all, :include => :categories, :order => "posts.id") + assert_equal 2, posts[0].categories.size + assert_equal 1, posts[1].categories.size + assert_equal 0, posts[2].categories.size + assert posts[0].categories.include?(@technology) + assert posts[1].categories.include?(@general) end def test_eager_with_inheritance diff --git a/activerecord/test/fixtures/posts.yml b/activerecord/test/fixtures/posts.yml index 037f0a2323..decd14b70e 100644 --- a/activerecord/test/fixtures/posts.yml +++ b/activerecord/test/fixtures/posts.yml @@ -14,6 +14,7 @@ thinking: authorless: id: 3 + author_id: 0 title: I don't have any comments body: I just don't want to type: Post |