aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb1
-rwxr-xr-xactiverecord/lib/active_record/base.rb2
-rw-r--r--activerecord/test/finder_test.rb9
4 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index a4130b8a5f..9ea6ff93bc 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Move from select * to select tablename.* to avoid clobbering IDs. Closes #8889 [dasil003]
+
* Don't call unsupported methods on associated objects when using :include, :method with to_xml #7307, [manfred, jwilger]
* Define collection singular ids method for has_many :through associations. #8763 [lifofifo]
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index 2155ce8e30..b5f718ae52 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -53,6 +53,7 @@ module ActiveRecord
options[:conditions] = conditions
options[:joins] = @join_sql
options[:readonly] = finding_with_ambigious_select?(options[:select])
+ options[:select] ||= '*'
if options[:order] && @reflection.options[:order]
options[:order] = "#{options[:order]}, #{@reflection.options[:order]}"
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 77b22aad6a..5ab761932f 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -1109,7 +1109,7 @@ module ActiveRecord #:nodoc:
def construct_finder_sql(options)
scope = scope(:find)
- sql = "SELECT #{(scope && scope[:select]) || options[:select] || '*'} "
+ sql = "SELECT #{(scope && scope[:select]) || options[:select] || (options[:joins] && table_name + '.*') || '*'} "
sql << "FROM #{(scope && scope[:from]) || options[:from] || table_name} "
add_joins!(sql, options, scope)
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index c32ceaa466..9c68b604cc 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -507,6 +507,15 @@ class FinderTest < Test::Unit::TestCase
assert developer_names.include?('Jamis')
end
+ def test_joins_dont_clobber_id
+ first = Firm.find(
+ :first,
+ :joins => 'INNER JOIN companies AS clients ON clients.firm_id = companies.id',
+ :conditions => 'companies.id = 1'
+ )
+ assert_equal 1, first.id
+ end
+
def test_find_by_id_with_conditions_with_or
assert_nothing_raised do
Post.find([1,2,3],