Browse files

Merge pull request #14372 from arthurnn/fix_query_cache_binds

Make select_all on query cache accept a Relation without binds.
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
1 parent 2ce5e08 commit cf578532454c5227318c189643edbaa9763c6633 @rafaelfranca rafaelfranca committed Mar 13, 2014
View
7 activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Add support for `Relation` be passed as parameter on `QueryCache#select_all`.
+
+ Fixes #14361.
+
+ *arthurnn*
+
+
## Rails 4.0.4 ##
* Only save has_one associations if record has changes.
View
21 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -21,14 +21,7 @@ def to_sql(arel, binds = [])
# Returns an array of record hashes with the column names as keys and
# column values as values.
def select_all(arel, name = nil, binds = [])
- if arel.is_a?(Relation)
- relation = arel
- arel = relation.arel
- if !binds || binds.empty?
- binds = relation.bind_values
- end
- end
-
+ arel, binds = binds_from_relation arel, binds
select(to_sql(arel, binds), name, binds)
end
@@ -49,10 +42,7 @@ def select_value(arel, name = nil, binds = [])
# Returns an array of the values of the first column in a select:
# select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
def select_values(arel, name = nil)
- binds = []
- if arel.is_a?(Relation)
- arel, binds = arel.arel, arel.bind_values
- end
+ arel, binds = binds_from_relation arel
select_rows(to_sql(arel, binds), name, binds).map(&:first)
end
@@ -396,6 +386,13 @@ def last_inserted_id(result)
row = result.rows.first
row && row.first
end
+
+ def binds_from_relation(relation, binds = [])
+ if relation.is_a?(Relation) && binds.blank?
+ relation, binds = relation.arel, relation.bind_values
+ end
+ [relation, binds]
+ end
end
end
end
View
1 activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
@@ -57,6 +57,7 @@ def clear_query_cache
def select_all(arel, name = nil, binds = [])
if @query_cache_enabled && !locked?(arel)
+ arel, binds = binds_from_relation arel, binds
sql = to_sql(arel, binds)
cache_sql(sql, binds) { super(sql, name, binds) }
else
View
8 activerecord/test/cases/query_cache_test.rb
@@ -118,6 +118,14 @@ def test_cache_clear_after_close
assert ActiveRecord::Base.connection.query_cache.empty?, 'cache should be empty'
end
+ def test_cache_passing_a_relation
+ post = Post.first
+ Post.cache do
+ query = post.categories.select(:post_id)
+ assert Post.connection.select_all(query).is_a?(ActiveRecord::Result)
+ end
+ end
+
def test_find_queries
assert_queries(2) { Task.find(1); Task.find(1) }
end

0 comments on commit cf57853

Please sign in to comment.