Permalink
Browse files

Ensure :select passed in options overrides the one from the scope. [#239

 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent cf6299d commit 235d635708dd72bee0828457af5397c79750483a @pixeltrix pixeltrix committed with lifo May 29, 2008
Showing with 17 additions and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/base.rb
  2. +16 −0 activerecord/test/cases/method_scoping_test.rb
@@ -1457,7 +1457,7 @@ def type_name_with_module(type_name)
def construct_finder_sql(options)
scope = scope(:find)
- sql = "SELECT #{(scope && scope[:select]) || options[:select] || (options[:joins] && quoted_table_name + '.*') || '*'} "
+ sql = "SELECT #{options[:select] || (scope && scope[:select]) || (options[:joins] && quoted_table_name + '.*') || '*'} "
sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
add_joins!(sql, options, scope)
@@ -50,6 +50,22 @@ def test_scoped_find_all
end
end
+ def test_scoped_find_select
+ Developer.with_scope(:find => { :select => "id, name" }) do
+ developer = Developer.find(:first, :conditions => "name = 'David'")
+ assert_equal "David", developer.name
+ assert !developer.has_attribute?(:salary)
+ end
+ end
+
+ def test_options_select_replaces_scope_select
+ Developer.with_scope(:find => { :select => "id, name" }) do
+ developer = Developer.find(:first, :select => 'id, salary', :conditions => "name = 'David'")
+ assert_equal 80000, developer.salary
+ assert !developer.has_attribute?(:name)
+ end
+ end
+
def test_scoped_count
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
assert_equal 1, Developer.count

0 comments on commit 235d635

Please sign in to comment.