Permalink
Browse files

Move from select * to select tablename.* to avoid clobbering IDs. Clo…

…ses #8889 [dasil003]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent fd65d89 commit 3fbfc13ee35e074b3efd69c8d6dfeafd461dd43c @NZKoz NZKoz committed Jul 7, 2007
View
@@ -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]
@@ -53,6 +53,7 @@ def find(*args)
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]}"
@@ -1109,7 +1109,7 @@ def type_name_with_module(type_name)
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)
@@ -507,6 +507,15 @@ def test_find_all_with_join
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],

0 comments on commit 3fbfc13

Please sign in to comment.