diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 691c684d760b95cbddf8d3a61a455f85f0da66fb..05eb2f875003ee01479689f9cb37237cb4d5fcbb 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* + * Fix inserts with prepared statements disabled. Fixes #12023. diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 1c6ea94c0bf010d8fc6ca74c6783d30b1fcaba90..9916c597eecadfe603a3987ce58d73b450321df6 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -915,6 +915,7 @@ def build_from 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 b3ca45c4c8d0c03dc89be0b7b1ec8da26e01b291..88a12c61dfd0c64aa5c6cb7c4c970ce07b5b5bf5 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -139,6 +139,13 @@ def test_finding_with_subquery 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)