public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Ensure :select passed in options overrides the one from the scope. [#239 
state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
pixeltrix (author)
Thu May 29 04:59:29 -0700 2008
lifo (committer)
Thu May 29 06:10:24 -0700 2008
commit  235d635708dd72bee0828457af5397c79750483a
tree    51f09f956b8f4d84d6f0e81dd697d577ffcd7f92
parent  cf6299dbd73a8cb6d74265df03d01abe885e686a
...
1457
1458
1459
1460
 
1461
1462
1463
...
1457
1458
1459
 
1460
1461
1462
1463
0
@@ -1457,7 +1457,7 @@ module ActiveRecord #:nodoc:
0
 
0
         def construct_finder_sql(options)
0
           scope = scope(:find)
0
-          sql  = "SELECT #{(scope && scope[:select]) || options[:select] || (options[:joins] && quoted_table_name + '.*') || '*'} "
0
+          sql  = "SELECT #{options[:select] || (scope && scope[:select]) || (options[:joins] && quoted_table_name + '.*') || '*'} "
0
           sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
0
 
0
           add_joins!(sql, options, scope)
...
50
51
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
55
...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
0
@@ -50,6 +50,22 @@ class MethodScopingTest < ActiveRecord::TestCase
0
     end
0
   end
0
 
0
+  def test_scoped_find_select
0
+    Developer.with_scope(:find => { :select => "id, name" }) do
0
+      developer = Developer.find(:first, :conditions => "name = 'David'")
0
+      assert_equal "David", developer.name
0
+      assert !developer.has_attribute?(:salary)
0
+    end
0
+  end
0
+
0
+  def test_options_select_replaces_scope_select
0
+    Developer.with_scope(:find => { :select => "id, name" }) do
0
+      developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
0
+      assert_equal 80000, developer.salary
0
+      assert !developer.has_attribute?(:name)
0
+    end
0
+  end
0
+
0
   def test_scoped_count
0
     Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
0
       assert_equal 1, Developer.count

Comments