From 686e8fb52e00b24bd3a334cd7748df950914bfbf Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 10 Jun 2017 19:13:30 +0900 Subject: `Relation#locked?` should not build arel --- activerecord/lib/active_record/relation.rb | 2 +- activerecord/test/cases/relations_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 7a8f9abb36..133c1a6318 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -18,7 +18,7 @@ module ActiveRecord attr_reader :table, :klass, :loaded, :predicate_builder alias :model :klass alias :loaded? :loaded - alias :locked? :locked + alias :locked? :lock_value def initialize(klass, table, predicate_builder, values = {}) @klass = klass diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 86f3b0b962..5f36e4ec5d 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -2001,6 +2001,12 @@ class RelationTest < ActiveRecord::TestCase assert_equal binds, merged.bound_attributes end + def test_locked_should_not_build_arel + posts = Post.lock + assert posts.locked? + assert_nothing_raised { posts.lock!(false) } + end + def test_relation_join_method assert_equal "Thank you for the welcome,Thank you again for the welcome", Post.first.comments.join(",") end -- cgit v1.2.3 From 425f2cacafc522bb0aa426a0a4bc92c1237160aa Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Thu, 29 Jun 2017 02:11:28 +0900 Subject: Remove delegating to arel in a relation The delegation was needed since passing `relation` with `relation.bound_attributes`. It should use `relation.arel` in that case. --- .../lib/active_record/connection_adapters/abstract/query_cache.rb | 1 + activerecord/lib/active_record/relation/calculations.rb | 2 +- activerecord/lib/active_record/relation/delegation.rb | 2 -- activerecord/lib/active_record/relation/finder_methods.rb | 8 +++----- activerecord/test/cases/relations_test.rb | 2 +- activerecord/test/models/post.rb | 1 + 6 files changed, 7 insertions(+), 9 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb index e53ba4e666..33695c0537 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb @@ -123,6 +123,7 @@ module ActiveRecord # 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 diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index c562f214c9..24b8be0242 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -311,7 +311,7 @@ module ActiveRecord 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] } diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb index dada0fddf8..8b4dd25689 100644 --- a/activerecord/lib/active_record/relation/delegation.rb +++ b/activerecord/lib/active_record/relation/delegation.rb @@ -44,8 +44,6 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 1d661fa8ed..47f42bf4ca 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -316,7 +316,7 @@ module ActiveRecord 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 @@ -377,8 +377,7 @@ module ActiveRecord 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 @@ -425,9 +424,8 @@ module ActiveRecord "#{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 diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 5f36e4ec5d..84ccfbf5d5 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -2002,7 +2002,7 @@ class RelationTest < ActiveRecord::TestCase end def test_locked_should_not_build_arel - posts = Post.lock + posts = Post.locked assert posts.locked? assert_nothing_raised { posts.lock!(false) } end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 4c913b3b72..ed64e0ee52 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -22,6 +22,7 @@ class Post < ActiveRecord::Base 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 -- cgit v1.2.3