aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/association_preload.rb7
-rw-r--r--[-rwxr-xr-x]activerecord/lib/active_record/associations.rb25
-rw-r--r--activerecord/test/cases/associations/eager_test.rb4
-rw-r--r--activerecord/test/cases/finder_test.rb9
-rw-r--r--activerecord/test/models/post.rb2
5 files changed, 45 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index 3e7c787dee..da4ebdef51 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -65,7 +65,13 @@ module ActiveRecord
end
def set_association_single_records(id_to_record_map, reflection_name, associated_records, key)
+ seen_keys = {}
associated_records.each do |associated_record|
+ #this is a has_one or belongs_to: there should only be one record.
+ #Unfortunately we can't (in portable way) ask the database for 'all records where foo_id in (x,y,z), but please
+ # only one row per distinct foo_id' so this where we enforce that
+ next if seen_keys[associated_record[key].to_s]
+ seen_keys[associated_record[key].to_s] = true
mapped_records = id_to_record_map[associated_record[key].to_s]
mapped_records.each do |mapped_record|
mapped_record.send("set_#{reflection_name}_target", associated_record)
@@ -122,7 +128,6 @@ module ActiveRecord
else
records.each {|record| record.send("set_#{reflection.name}_target", nil)}
-
set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
end
end
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 5251a7b0f5..691d938788 100755..100644
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1446,6 +1446,12 @@ module ActiveRecord
tables_from_conditions = conditions_tables(options)
tables_from_order = order_tables(options)
all_tables = tables_from_conditions + tables_from_order
+ distinct_join_associations = all_tables.uniq.map{|table|
+ join_dependency.joins_for_table_name(table)
+ }.flatten.compact.uniq
+
+
+
is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order)
sql = "SELECT "
@@ -1457,7 +1463,7 @@ module ActiveRecord
sql << " FROM #{connection.quote_table_name table_name} "
if is_distinct
- sql << join_dependency.join_associations.reject{ |ja| !all_tables.include?(ja.table_name) }.collect(&:association_join).join
+ sql << distinct_join_associations.collect(&:association_join).join
add_joins!(sql, options, scope)
end
@@ -1617,6 +1623,23 @@ module ActiveRecord
end
end
+ def join_for_table_name(table_name)
+ @joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil
+ end
+
+ def joins_for_table_name(table_name)
+ join = join_for_table_name(table_name)
+ result = nil
+ if join && join.is_a?(JoinAssociation)
+ result = [join]
+ if join.parent && join.parent.is_a?(JoinAssociation)
+ result = joins_for_table_name(join.parent.aliased_table_name) +
+ result
+ end
+ end
+ result
+ end
+
protected
def build(associations, parent = nil)
parent ||= @joins.last
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index 546ed80894..67b57ceb42 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -29,6 +29,10 @@ class EagerAssociationTest < ActiveRecord::TestCase
post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
assert_equal 2, post.comments.size
assert post.comments.include?(comments(:greetings))
+
+ posts = Post.find(:all, :include => :last_comment)
+ post = posts.find { |p| p.id == 1 }
+ assert_equal Post.find(1).last_comment, post.last_comment
end
def test_loading_conditions_with_or
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index b7f87fe6e8..2acfe9b387 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -8,6 +8,7 @@ require 'models/entrant'
require 'models/developer'
require 'models/post'
require 'models/customer'
+require 'models/job'
class FinderTest < ActiveRecord::TestCase
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers
@@ -857,6 +858,14 @@ class FinderTest < ActiveRecord::TestCase
Company.connection.select_rows("SELECT id, name FROM companies WHERE id IN (1,2,3) ORDER BY id").map! {|i| i.map! {|j| j.to_s unless j.nil?}}
end
+ def test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
+ assert_equal 2, Post.find(:all,:include=>{:authors=>:author_address},:order=>' author_addresses.id DESC ', :limit=>2).size
+
+ assert_equal 3, Post.find(:all,:include=>{:author=>:author_address,:authors=>:author_address},
+ :order=>' author_addresses_authors.id DESC ', :limit=>3).size
+ end
+
+
protected
def bind(statement, *vars)
if vars.first.is_a?(Hash)
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 22c5a645b8..d9101706b5 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -9,6 +9,8 @@ class Post < ActiveRecord::Base
belongs_to :author_with_posts, :class_name => "Author", :foreign_key => :author_id, :include => :posts
+ has_one :last_comment, :class_name => 'Comment', :order => 'id desc'
+
has_many :comments, :order => "body" do
def find_most_recent
find(:first, :order => "id DESC")