aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCody Cutrer <cody@instructure.com>2013-12-16 21:26:37 -0700
committerCody Cutrer <cody@cutrer.us>2013-12-19 14:40:13 -0700
commit847e9a95da59f1263e6e5c15cd5ce5c9ec8260a6 (patch)
treec47923c5e2de4efd9e12dc0098462f1c491883ad
parente4cde5d58cbb09d1843796f96ba86225ff94fe05 (diff)
downloadrails-847e9a95da59f1263e6e5c15cd5ce5c9ec8260a6.tar.gz
rails-847e9a95da59f1263e6e5c15cd5ce5c9ec8260a6.tar.bz2
rails-847e9a95da59f1263e6e5c15cd5ce5c9ec8260a6.zip
fix default select when from is used
-rw-r--r--activerecord/CHANGELOG.md18
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb4
-rw-r--r--activerecord/test/cases/relations_test.rb5
3 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 52f5f0efa3..149a3e6fc4 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,21 @@
+* Improve the default select when from is used.
+
+ Previously, if you did something like Topic.from(:temp_topics), it
+ would generate SQL like:
+
+ SELECT topics.* FROM temp_topics;
+
+ Which is useless, cause obviously there's not a topics table to select
+ from. So one would always have to include a select to override the
+ default behavior. Now the default if you use from is just *:
+
+ SELECT * FROM temp_topics;
+
+ Which may not be what you want in all cases, but is at least usable
+ in some cases.
+
+ *Cody Cutrer*
+
* Fix `PostgreSQL` insert to properly extract table name from multiline string SQL.
Previously, executing an insert SQL in `PostgreSQL` with a command like this:
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index e7b8809fe0..3d0709266a 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -982,8 +982,10 @@ module ActiveRecord
end
def build_select(arel, selects)
- unless selects.empty?
+ if !selects.empty?
arel.project(*selects)
+ elsif from_value
+ arel.project(Arel.star)
else
arel.project(@klass.arel_table[Arel.star])
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 2ccf4c7578..031da8e6d6 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -151,6 +151,11 @@ class RelationTest < ActiveRecord::TestCase
assert_equal relation.to_a, Comment.select('a.*').from(relation, :a).to_a
end
+ def test_finding_with_subquery_without_select
+ relation = Topic.where(:approved => true)
+ assert_equal relation.to_a, Topic.from(relation).to_a
+ end
+
def test_finding_with_conditions
assert_equal ["David"], Author.where(:name => 'David').map(&:name)
assert_equal ['Mary'], Author.where(["name = ?", 'Mary']).map(&:name)