Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added :select option to find which can specify a different value than…

… the default *, like find(:all, :select => "first_name, last_name"), if you either only want to select part of the columns or exclude columns otherwise included from a join #1338 [Stefan Kaes]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1830 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 6f05696ceb2fe21b735c428c96c228705fc4c6c5 1 parent a7cca4e
@dhh dhh authored
View
5 activerecord/CHANGELOG
@@ -1,3 +1,8 @@
+*SVN*
+
+* Added :select option to find which can specify a different value than the default *, like find(:all, :select => "first_name, last_name"), if you either only want to select part of the columns or exclude columns otherwise included from a join #1338 [Stefan Kaes]
+
+
*1.11.1* (11 July, 2005)
* Added support for limit and offset with eager loading of has_one and belongs_to associations. Using the options with has_many and has_and_belongs_to_many associations will now raise an ActiveRecord::ConfigurationError #1692 [Rick Olsen]
View
4 activerecord/lib/active_record/base.rb
@@ -315,6 +315,8 @@ class << self # Class methods
# * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
# * <tt>:include</tt>: Names associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
# to already defined associations. See eager loading under Associations.
+ # * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
+ # include the joined columns.
#
# Examples for find by id:
# Person.find(1) # returns the object for ID = 1
@@ -739,7 +741,7 @@ def type_name_with_module(type_name)
end
def construct_finder_sql(options)
- sql = "SELECT * FROM #{table_name} "
+ sql = "SELECT #{options[:select] || '*'} FROM #{table_name} "
sql << " #{options[:joins]} " if options[:joins]
add_conditions!(sql, options[:conditions])
sql << "ORDER BY #{options[:order]} " if options[:order]
View
4 activerecord/test/finder_test.rb
@@ -93,6 +93,10 @@ def test_unexisting_record_exception_handling
Topic.find(2).parent
end
+
+ def test_find_only_some_columns
+ assert_raises(NoMethodError) { Topic.find(1, :select => "author_name").title }
+ end
def test_find_on_conditions
assert Topic.find(1, :conditions => "approved = 0")
Please sign in to comment.
Something went wrong with that request. Please try again.