Skip to content

Commit

Permalink
Remove delegating to arel in a relation
Browse files Browse the repository at this point in the history
The delegation was needed since passing `relation` with
`relation.bound_attributes`. It should use `relation.arel` in that case.
  • Loading branch information
kamipo committed Jun 28, 2017
1 parent 686e8fb commit 425f2ca
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def cache_sql(sql, name, binds)
# If arel is locked this is a SELECT ... FOR UPDATE or somesuch. Such
# queries should not be cached.
def locked?(arel)
arel = arel.arel if arel.is_a?(Relation)
arel.respond_to?(:locked) && arel.locked
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
relation.group_values = group_fields
relation.select_values = select_values

calculated_data = @klass.connection.select_all(relation, nil, relation.bound_attributes)
calculated_data = @klass.connection.select_all(relation.arel, nil, relation.bound_attributes)

if association
key_ids = calculated_data.collect { |row| row[group_aliases.first] }
Expand Down
2 changes: 0 additions & 2 deletions activerecord/lib/active_record/relation/delegation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ def inherited(child_class)
delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key,
:connection, :columns_hash, to: :klass

delegate :ast, :locked, to: :arel

module ClassSpecificRelation # :nodoc:
extend ActiveSupport::Concern

Expand Down
8 changes: 3 additions & 5 deletions activerecord/lib/active_record/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def exists?(conditions = :none)

relation = construct_relation_for_exists(relation, conditions)

connection.select_value(relation, "#{name} Exists", relation.bound_attributes) ? true : false
connection.select_value(relation.arel, "#{name} Exists", relation.bound_attributes) ? true : false
rescue ::RangeError
false
end
Expand Down Expand Up @@ -377,8 +377,7 @@ def find_with_associations
if ActiveRecord::NullRelation === relation
[]
else
arel = relation.arel
rows = connection.select_all(arel, "SQL", relation.bound_attributes)
rows = connection.select_all(relation.arel, "SQL", relation.bound_attributes)
join_dependency.instantiate(rows, aliases)
end
end
Expand Down Expand Up @@ -425,9 +424,8 @@ def limited_ids_for(relation)
"#{quoted_table_name}.#{quoted_primary_key}", relation.order_values)

relation = relation.except(:select).select(values).distinct!
arel = relation.arel

id_rows = @klass.connection.select_all(arel, "SQL", relation.bound_attributes)
id_rows = @klass.connection.select_all(relation.arel, "SQL", relation.bound_attributes)
id_rows.map { |row| row[primary_key] }
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ def test_merging_keeps_lhs_binds
end

def test_locked_should_not_build_arel
posts = Post.lock
posts = Post.locked
assert posts.locked?
assert_nothing_raised { posts.lock!(false) }
end
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def greeting
scope :ranked_by_comments, -> { order("comments_count DESC") }

scope :limit_by, lambda { |l| limit(l) }
scope :locked, -> { lock }

belongs_to :author
belongs_to :readonly_author, -> { readonly }, class_name: "Author", foreign_key: :author_id
Expand Down

0 comments on commit 425f2ca

Please sign in to comment.