Skip to content

Commit

Permalink
Ensure :select passed in options overrides the one from the scope. [#239
Browse files Browse the repository at this point in the history
 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
pixeltrix authored and lifo committed May 29, 2008
1 parent cf6299d commit 235d635
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/method_scoping_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 235d635

Please sign in to comment.