aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/association.rb7
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/join_dependency.rb10
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_base.rb15
-rw-r--r--activerecord/lib/active_record/associations/join_dependency/join_part.rb5
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb12
-rw-r--r--activerecord/lib/active_record/associations/singular_association.rb5
-rw-r--r--activerecord/lib/active_record/attribute.rb2
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb3
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb99
-rw-r--r--activerecord/lib/active_record/attribute_mutation_tracker.rb9
-rw-r--r--activerecord/lib/active_record/attribute_set.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql/quoting.rb8
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb4
-rw-r--r--activerecord/lib/active_record/core.rb12
-rw-r--r--activerecord/lib/active_record/model_schema.rb1
-rw-r--r--activerecord/lib/active_record/persistence.rb6
-rw-r--r--activerecord/lib/active_record/railtie.rb16
-rw-r--r--activerecord/lib/active_record/reflection.rb8
-rw-r--r--activerecord/lib/active_record/relation.rb9
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb4
-rw-r--r--activerecord/lib/active_record/relation/merger.rb7
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb13
-rw-r--r--activerecord/lib/active_record/relation/where_clause.rb4
-rw-r--r--activerecord/lib/active_record/scoping.rb5
26 files changed, 94 insertions, 180 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index 1138ae3462..c166cfd68f 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -171,8 +171,8 @@ module ActiveRecord
skip_assign = [reflection.foreign_key, reflection.type].compact
assigned_keys = record.changed_attribute_names_to_save
assigned_keys += except_from_scope_attributes.keys.map(&:to_s)
- attributes = create_scope.except(*(assigned_keys - skip_assign))
- record.assign_attributes(attributes)
+ attributes = scope_for_create.except!(*(assigned_keys - skip_assign))
+ record.send(:_assign_attributes, attributes) if attributes.any?
set_inverse_instance(record)
end
@@ -185,6 +185,9 @@ module ActiveRecord
end
private
+ def scope_for_create
+ scope.scope_for_create
+ end
def find_target?
!loaded? && (!owner.new_record? || foreign_key_present?) && klass
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index a49fb155ee..69401162aa 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -374,10 +374,6 @@ module ActiveRecord
end
end
- def create_scope
- scope.scope_for_create.stringify_keys
- end
-
def delete_or_destroy(records, method)
records = records.flatten
records.each { |record| raise_on_type_mismatch!(record) }
diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb
index 04cdcb6a7f..4a3fb6eaec 100644
--- a/activerecord/lib/active_record/associations/join_dependency.rb
+++ b/activerecord/lib/active_record/associations/join_dependency.rb
@@ -33,12 +33,8 @@ module ActiveRecord
end
Table = Struct.new(:node, :columns) do # :nodoc:
- def table
- Arel::Nodes::TableAlias.new node.table, node.aliased_table_name
- end
-
def column_aliases
- t = table
+ t = node.table
columns.map { |column| t[column.name].as Arel.sql column.alias }
end
end
@@ -92,11 +88,11 @@ module ActiveRecord
# associations # => [:appointments]
# joins # => []
#
- def initialize(base, associations, joins, eager_loading: true)
+ def initialize(base, table, associations, joins, eager_loading: true)
@alias_tracker = AliasTracker.create_with_joins(base.connection, base.table_name, joins)
@eager_loading = eager_loading
tree = self.class.make_tree associations
- @join_root = JoinBase.new base, build(tree, base)
+ @join_root = JoinBase.new(base, table, build(tree, base))
@join_root.children.each { |child| construct_tables! @join_root, child }
end
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
index b14ddfeeeb..98b2f40357 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb
@@ -57,10 +57,6 @@ module ActiveRecord
def table
tables.first
end
-
- def aliased_table_name
- table.table_alias || table.name
- end
end
end
end
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_base.rb b/activerecord/lib/active_record/associations/join_dependency/join_base.rb
index 6e0963425d..d4e26d5397 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_base.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_base.rb
@@ -4,17 +4,16 @@ module ActiveRecord
module Associations
class JoinDependency # :nodoc:
class JoinBase < JoinPart # :nodoc:
- def match?(other)
- return true if self == other
- super && base_klass == other.base_klass
- end
+ attr_reader :table
- def table
- base_klass.arel_table
+ def initialize(base_klass, table, children)
+ super(base_klass, children)
+ @table = table
end
- def aliased_table_name
- base_klass.table_name
+ def match?(other)
+ return true if self == other
+ super && base_klass == other.base_klass
end
end
end
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_part.rb b/activerecord/lib/active_record/associations/join_dependency/join_part.rb
index 80c9fde5d1..5666e069fc 100644
--- a/activerecord/lib/active_record/associations/join_dependency/join_part.rb
+++ b/activerecord/lib/active_record/associations/join_dependency/join_part.rb
@@ -36,11 +36,6 @@ module ActiveRecord
raise NotImplementedError
end
- # The alias for the active_record's table
- def aliased_table_name
- raise NotImplementedError
- end
-
def extract_record(row, column_names_with_alias)
# This code is performance critical as it is called per row.
# see: https://github.com/rails/rails/pull/12185
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb
index 85343040db..698fd29beb 100644
--- a/activerecord/lib/active_record/associations/preloader/association.rb
+++ b/activerecord/lib/active_record/associations/preloader/association.rb
@@ -114,8 +114,18 @@ module ActiveRecord
@reflection_scope ||= reflection.scope_for(klass)
end
+ def klass_scope
+ current_scope = klass.current_scope
+
+ if current_scope && current_scope.empty_scope?
+ klass.unscoped
+ else
+ klass.default_scoped
+ end
+ end
+
def build_scope
- scope = klass.default_scoped
+ scope = klass_scope
if reflection.type
scope.where!(reflection.type => model.base_class.sti_name)
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb
index f8bbe4c2ed..66993b9bf7 100644
--- a/activerecord/lib/active_record/associations/singular_association.rb
+++ b/activerecord/lib/active_record/associations/singular_association.rb
@@ -30,9 +30,8 @@ module ActiveRecord
end
private
-
- def create_scope
- scope.scope_for_create.stringify_keys.except(klass.primary_key)
+ def scope_for_create
+ super.except!(klass.primary_key)
end
def find_target
diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb
index 78662433eb..f89261923b 100644
--- a/activerecord/lib/active_record/attribute.rb
+++ b/activerecord/lib/active_record/attribute.rb
@@ -174,7 +174,7 @@ module ActiveRecord
end
def came_from_user?
- true
+ !type.value_constructed_by_mass_assignment?(value_before_type_cast)
end
end
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 83c61fad19..4e6a43a039 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -1,7 +1,4 @@
-require "active_support/core_ext/enumerable"
-require "active_support/core_ext/string/filters"
require "mutex_m"
-require "concurrent/map"
module ActiveRecord
# = Active Record Attribute Methods
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 07d194cc57..5efe051125 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -17,8 +17,8 @@ module ActiveRecord
class_attribute :partial_writes, instance_writer: false, default: true
- after_create { changes_internally_applied }
- after_update { changes_internally_applied }
+ after_create { changes_applied }
+ after_update { changes_applied }
# Attribute methods for "changed in last call to save?"
attribute_method_affix(prefix: "saved_change_to_", suffix: "?")
@@ -30,25 +30,10 @@ module ActiveRecord
attribute_method_suffix("_change_to_be_saved", "_in_database")
end
- # Attempts to +save+ the record and clears changed attributes if successful.
- def save(*)
- if status = super
- changes_applied
- end
- status
- end
-
- # Attempts to <tt>save!</tt> the record and clears changed attributes if successful.
- def save!(*)
- super.tap do
- changes_applied
- end
- end
-
# <tt>reload</tt> the record and clears changed attributes.
def reload(*)
super.tap do
- @previous_mutation_tracker = nil
+ @mutations_before_last_save = nil
clear_mutation_trackers
@changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
end
@@ -62,20 +47,16 @@ module ActiveRecord
clear_mutation_trackers
end
- def changes_internally_applied # :nodoc:
+ def changes_applied
@mutations_before_last_save = mutation_tracker
- forget_attribute_assignments
@mutations_from_database = AttributeMutationTracker.new(@attributes)
- end
-
- def changes_applied
- @previous_mutation_tracker = mutation_tracker
@changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
+ forget_attribute_assignments
clear_mutation_trackers
end
def clear_changes_information
- @previous_mutation_tracker = nil
+ @mutations_before_last_save = nil
@changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
forget_attribute_assignments
clear_mutation_trackers
@@ -100,28 +81,18 @@ module ActiveRecord
if defined?(@cached_changed_attributes)
@cached_changed_attributes
else
- emit_warning_if_needed("changed_attributes", "saved_changes.transform_values(&:first)")
super.reverse_merge(mutation_tracker.changed_values).freeze
end
end
def changes
cache_changed_attributes do
- emit_warning_if_needed("changes", "saved_changes")
super
end
end
def previous_changes
- unless previous_mutation_tracker.equal?(mutations_before_last_save)
- ActiveSupport::Deprecation.warn(<<-EOW.strip_heredoc)
- The behavior of `previous_changes` inside of after callbacks is
- deprecated without replacement. In the next release of Rails,
- this method inside of `after_save` will return the changes that
- were just saved.
- EOW
- end
- previous_mutation_tracker.changes
+ mutations_before_last_save.changes
end
def attribute_changed_in_place?(attr_name)
@@ -211,31 +182,6 @@ module ActiveRecord
changes_to_save.transform_values(&:first)
end
- def attribute_was(*)
- emit_warning_if_needed("attribute_was", "attribute_before_last_save")
- super
- end
-
- def attribute_change(*)
- emit_warning_if_needed("attribute_change", "saved_change_to_attribute")
- super
- end
-
- def attribute_changed?(*)
- emit_warning_if_needed("attribute_changed?", "saved_change_to_attribute?")
- super
- end
-
- def changed?(*)
- emit_warning_if_needed("changed?", "saved_changes?")
- super
- end
-
- def changed(*)
- emit_warning_if_needed("changed", "saved_changes.keys")
- super
- end
-
private
def mutation_tracker
@@ -245,18 +191,6 @@ module ActiveRecord
@mutation_tracker ||= AttributeMutationTracker.new(@attributes)
end
- def emit_warning_if_needed(method_name, new_method_name)
- unless mutation_tracker.equal?(mutations_from_database)
- ActiveSupport::Deprecation.warn(<<-EOW.squish)
- The behavior of `#{method_name}` inside of after callbacks will
- be changing in the next version of Rails. The new return value will reflect the
- behavior of calling the method after `save` returned (e.g. the opposite of what
- it returns now). To maintain the current behavior, use `#{new_method_name}`
- instead.
- EOW
- end
- end
-
def mutations_from_database
unless defined?(@mutations_from_database)
@mutations_from_database = nil
@@ -275,17 +209,7 @@ module ActiveRecord
def attribute_will_change!(attr_name)
super
- if self.class.has_attribute?(attr_name)
- mutations_from_database.force_change(attr_name)
- else
- ActiveSupport::Deprecation.warn(<<-EOW.squish)
- #{attr_name} is not an attribute known to Active Record.
- This behavior is deprecated and will be removed in the next
- version of Rails. If you'd like #{attr_name} to be managed
- by Active Record, add `attribute :#{attr_name}` to your class.
- EOW
- mutations_from_database.deprecated_force_change(attr_name)
- end
+ mutations_from_database.force_change(attr_name)
end
def _update_record(*)
@@ -307,15 +231,10 @@ module ActiveRecord
def clear_mutation_trackers
@mutation_tracker = nil
@mutations_from_database = nil
- @mutations_before_last_save = nil
- end
-
- def previous_mutation_tracker
- @previous_mutation_tracker ||= NullMutationTracker.instance
end
def mutations_before_last_save
- @mutations_before_last_save ||= previous_mutation_tracker
+ @mutations_before_last_save ||= NullMutationTracker.instance
end
def cache_changed_attributes
diff --git a/activerecord/lib/active_record/attribute_mutation_tracker.rb b/activerecord/lib/active_record/attribute_mutation_tracker.rb
index a01a58f8a5..e275ae72ba 100644
--- a/activerecord/lib/active_record/attribute_mutation_tracker.rb
+++ b/activerecord/lib/active_record/attribute_mutation_tracker.rb
@@ -5,7 +5,6 @@ module ActiveRecord
def initialize(attributes)
@attributes = attributes
@forced_changes = Set.new
- @deprecated_forced_changes = Set.new
end
def changed_values
@@ -33,7 +32,7 @@ module ActiveRecord
end
def any_changes?
- attr_names.any? { |attr| changed?(attr) } || deprecated_forced_changes.any?
+ attr_names.any? { |attr| changed?(attr) }
end
def changed?(attr_name, from: OPTION_NOT_GIVEN, to: OPTION_NOT_GIVEN)
@@ -62,15 +61,11 @@ module ActiveRecord
forced_changes << attr_name.to_s
end
- def deprecated_force_change(attr_name)
- deprecated_forced_changes << attr_name.to_s
- end
-
# TODO Change this to private once we've dropped Ruby 2.2 support.
# Workaround for Ruby 2.2 "private attribute?" warning.
protected
- attr_reader :attributes, :forced_changes, :deprecated_forced_changes
+ attr_reader :attributes, :forced_changes
private
diff --git a/activerecord/lib/active_record/attribute_set.rb b/activerecord/lib/active_record/attribute_set.rb
index 6399e3de70..a963ba015a 100644
--- a/activerecord/lib/active_record/attribute_set.rb
+++ b/activerecord/lib/active_record/attribute_set.rb
@@ -98,8 +98,6 @@ module ActiveRecord
attributes == other.attributes
end
- # TODO Change this to private once we've dropped Ruby 2.2 support.
- # Workaround for Ruby 2.2 "private attribute?" warning.
protected
attr_reader :attributes
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb b/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb
index d4f5906b33..0cc0ac74fe 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb
@@ -39,6 +39,14 @@ module ActiveRecord
def quoted_binary(value)
"x'#{value.hex}'"
end
+
+ def _type_cast(value)
+ case value
+ when Type::Time::Value then value.__getobj__
+ when Date, Time then value
+ else super
+ end
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index b79dbe0733..2fede7b847 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -82,11 +82,11 @@ module ActiveRecord
# Conversion can be accomplished by setting up a rake task which runs
#
# ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
- # ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 0)
+ # ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
# for all models and all boolean columns, after which the flag must be set
# to true by adding the following to your application.rb file:
#
- # ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
+ # Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
class_attribute :represent_boolean_as_integer, default: false
class StatementPool < ConnectionAdapters::StatementPool
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 198c712abc..3ea7b644c2 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -1,6 +1,4 @@
-require "thread"
require "active_support/core_ext/hash/indifferent_access"
-require "active_support/core_ext/object/duplicable"
require "active_support/core_ext/string/filters"
module ActiveRecord
@@ -265,16 +263,6 @@ module ActiveRecord
@arel_table ||= Arel::Table.new(table_name, type_caster: type_caster)
end
- # Returns the Arel engine.
- def arel_engine # :nodoc:
- @arel_engine ||=
- if Base == self || connection_handler.retrieve_connection_pool(connection_specification_name)
- self
- else
- superclass.arel_engine
- end
- end
-
def arel_attribute(name, table = arel_table) # :nodoc:
name = attribute_alias(name) if attribute_alias?(name)
table[name]
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 14e0f5bff7..2d3c9175bd 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -467,7 +467,6 @@ module ActiveRecord
end
def reload_schema_from_cache
- @arel_engine = nil
@arel_table = nil
@column_names = nil
@attribute_types = nil
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index a9509e562a..2e6901eb0e 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -175,7 +175,7 @@ module ActiveRecord
# callbacks or any <tt>:dependent</tt> association
# options, use <tt>#destroy</tt>.
def delete
- self.class.delete(id) if persisted?
+ _relation_for_itself.delete_all if persisted?
@destroyed = true
freeze
end
@@ -555,6 +555,10 @@ module ActiveRecord
end
def relation_for_destroy
+ _relation_for_itself
+ end
+
+ def _relation_for_itself
self.class.unscoped.where(self.class.primary_key => id)
end
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 962ed880b9..0b82926081 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -26,6 +26,9 @@ module ActiveRecord
config.active_record.use_schema_cache_dump = true
config.active_record.maintain_test_schema = true
+ config.active_record.sqlite3 = ActiveSupport::OrderedOptions.new
+ config.active_record.sqlite3.represent_boolean_as_integer = nil
+
config.eager_load_namespaces << ActiveRecord
rake_tasks do
@@ -108,7 +111,9 @@ module ActiveRecord
initializer "active_record.set_configs" do |app|
ActiveSupport.on_load(:active_record) do
- app.config.active_record.each do |k, v|
+ configs = app.config.active_record.dup
+ configs.delete(:sqlite3)
+ configs.each do |k, v|
send "#{k}=", v
end
end
@@ -178,6 +183,11 @@ end_warning
initializer "active_record.check_represent_sqlite3_boolean_as_integer" do
config.after_initialize do
ActiveSupport.on_load(:active_record_sqlite3adapter) do
+ represent_boolean_as_integer = Rails.application.config.active_record.sqlite3.delete(:represent_boolean_as_integer)
+ unless represent_boolean_as_integer.nil?
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = represent_boolean_as_integer
+ end
+
unless ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
ActiveSupport::Deprecation.warn <<-MSG
Leaving `ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer`
@@ -187,12 +197,12 @@ serialization) before setting this flag to true. Conversion can be accomplished
by setting up a rake task which runs
ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
- ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 0)
+ ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
for all models and all boolean columns, after which the flag must be set to
true by adding the following to your application.rb file:
- ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer = true
+ Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
MSG
end
end
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index a453ca55c7..1026e20f79 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -219,10 +219,10 @@ module ActiveRecord
end
def klass_join_scope(table, predicate_builder) # :nodoc:
- if klass.current_scope
- klass.current_scope.clone.tap { |scope|
- scope.joins_values = scope.left_outer_joins_values = [].freeze
- }
+ current_scope = klass.current_scope
+
+ if current_scope && current_scope.empty_scope?
+ build_scope(table, predicate_builder)
else
klass.default_scoped(build_scope(table, predicate_builder))
end
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 76cf47a3ed..d9e1e5c39c 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -556,8 +556,7 @@ module ActiveRecord
end
def reset
- @last = @to_sql = @order_clause = @scope_for_create = @arel = @loaded = nil
- @should_eager_load = @join_dependency = nil
+ @to_sql = @arel = @loaded = @should_eager_load = nil
@records = [].freeze
@offsets = {}
self
@@ -591,7 +590,7 @@ module ActiveRecord
end
def scope_for_create
- @scope_for_create ||= where_values_hash.merge(create_with_value)
+ where_values_hash.merge!(create_with_value.stringify_keys)
end
# Returns true if relation needs eager loading.
@@ -643,6 +642,10 @@ module ActiveRecord
"#<#{self.class.name} [#{entries.join(', ')}]>"
end
+ def empty_scope? # :nodoc:
+ @values == klass.unscoped.values
+ end
+
protected
def load_records(records)
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index ac0b4f597e..57b7d3c2e9 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -329,7 +329,7 @@ module ActiveRecord
# the expected number of results should be provided in the +expected_size+
# argument.
def raise_record_not_found_exception!(ids = nil, result_size = nil, expected_size = nil, key = primary_key) # :nodoc:
- conditions = arel.where_sql(@klass.arel_engine)
+ conditions = arel.where_sql(@klass)
conditions = " [#{conditions}]" if conditions
name = @klass.name
@@ -397,7 +397,7 @@ module ActiveRecord
def construct_join_dependency(joins = [], eager_loading: true)
including = eager_load_values + includes_values
- ActiveRecord::Associations::JoinDependency.new(@klass, including, joins, eager_loading: eager_loading)
+ ActiveRecord::Associations::JoinDependency.new(klass, table, including, joins, eager_loading: eager_loading)
end
def construct_relation_for_association_calculations
diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb
index 5dac00724a..6251f6fc33 100644
--- a/activerecord/lib/active_record/relation/merger.rb
+++ b/activerecord/lib/active_record/relation/merger.rb
@@ -119,9 +119,10 @@ module ActiveRecord
end
end
- join_dependency = ActiveRecord::Associations::JoinDependency.new(other.klass,
- joins_dependency,
- [])
+ join_dependency = ActiveRecord::Associations::JoinDependency.new(
+ other.klass, other.table, joins_dependency, []
+ )
+
relation.joins! rest
@relation = relation.joins join_dependency
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 79495ead91..efb55391e7 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -202,12 +202,13 @@ module ActiveRecord
# Works in two unique ways.
#
- # First: takes a block so it can be used just like +Array#select+.
+ # First: takes a block so it can be used just like <tt>Array#select</tt>.
#
# Model.all.select { |m| m.field == value }
#
# This will build an array of objects from the database for the scope,
- # converting them into an array and iterating through them using +Array#select+.
+ # converting them into an array and iterating through them using
+ # <tt>Array#select</tt>.
#
# Second: Modifies the SELECT statement for the query so that only certain
# fields are retrieved:
@@ -797,7 +798,7 @@ module ActiveRecord
value = sanitize_forbidden_attributes(value)
self.create_with_value = create_with_value.merge(value)
else
- self.create_with_value = {}
+ self.create_with_value = FROZEN_EMPTY_HASH
end
self
@@ -1019,9 +1020,7 @@ module ActiveRecord
join_list = join_nodes + convert_join_strings_to_ast(manager, string_joins)
join_dependency = ActiveRecord::Associations::JoinDependency.new(
- @klass,
- association_joins,
- join_list
+ klass, table, association_joins, join_list
)
join_infos = join_dependency.join_constraints stashed_association_joins, join_type
@@ -1047,7 +1046,7 @@ module ActiveRecord
if select_values.any?
arel.project(*arel_columns(select_values.uniq))
else
- arel.project(@klass.arel_table[Arel.star])
+ arel.project(table[Arel.star])
end
end
diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb
index 119910ee79..0feb2c6cb2 100644
--- a/activerecord/lib/active_record/relation/where_clause.rb
+++ b/activerecord/lib/active_record/relation/where_clause.rb
@@ -52,8 +52,8 @@ module ActiveRecord
binds = self.binds.map { |attr| [attr.name, attr.value] }.to_h
equalities.map { |node|
- name = node.left.name
- [name, binds.fetch(name.to_s) {
+ name = node.left.name.to_s
+ [name, binds.fetch(name) {
case node.right
when Array then node.right.map(&:val)
when Arel::Nodes::Casted, Arel::Nodes::Quoted
diff --git a/activerecord/lib/active_record/scoping.rb b/activerecord/lib/active_record/scoping.rb
index 94e0ef6724..82e20a32d0 100644
--- a/activerecord/lib/active_record/scoping.rb
+++ b/activerecord/lib/active_record/scoping.rb
@@ -33,9 +33,8 @@ module ActiveRecord
def populate_with_current_scope_attributes # :nodoc:
return unless self.class.scope_attributes?
- self.class.scope_attributes.each do |att, value|
- send("#{att}=", value) if respond_to?("#{att}=")
- end
+ attributes = self.class.scope_attributes
+ _assign_attributes(attributes) if attributes.any?
end
def initialize_internals_callback # :nodoc: