aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyan Wallace <rywall@gmail.com>2013-09-06 15:16:39 -0700
committerRyan Wallace <rywall@gmail.com>2013-09-08 14:00:19 -0700
commit87a84d3263c8712f85e70c45803203fa54658a9e (patch)
tree7453b761441cd4479e58794effc342bdb76f6ff4 /activerecord
parent6a91a3307ff6556225ab8717617074cea20222e0 (diff)
downloadrails-87a84d3263c8712f85e70c45803203fa54658a9e.tar.gz
rails-87a84d3263c8712f85e70c45803203fa54658a9e.tar.bz2
rails-87a84d3263c8712f85e70c45803203fa54658a9e.zip
Allow Relation#from to accept other relations with bind values.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb1
-rw-r--r--activerecord/test/cases/relations_test.rb7
3 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index b79b9a87c6..b5265a02bd 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Allow Relation#from to accept other relations with bind values.
+
+ *Ryan Wallace*
+
* Re-use `order` argument pre-processing for `reorder`.
*Paul Nikitochkin*
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 1c6ea94c0b..9916c597ee 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -915,6 +915,7 @@ module ActiveRecord
case opts
when Relation
name ||= 'subquery'
+ self.bind_values = opts.bind_values + self.bind_values
opts.arel.as(name.to_s)
else
opts
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index b3ca45c4c8..88a12c61df 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -139,6 +139,13 @@ class RelationTest < ActiveRecord::TestCase
assert_equal relation.to_a, Topic.select('a.*').from(relation, :a).to_a
end
+ def test_finding_with_subquery_with_binds
+ relation = Post.first.comments
+ assert_equal relation.to_a, Comment.select('*').from(relation).to_a
+ assert_equal relation.to_a, Comment.select('subquery.*').from(relation).to_a
+ assert_equal relation.to_a, Comment.select('a.*').from(relation, :a).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)