mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #13344 from ccutrer/fix-from-default-select
fix default select when from is used
This commit is contained in:
commit
3ea8403554
3 changed files with 26 additions and 1 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue