activerecord 6.1.0 → 6.1.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -15
  3. data/README.rdoc +1 -1
  4. data/lib/active_record/aggregations.rb +4 -4
  5. data/lib/active_record/association_relation.rb +10 -0
  6. data/lib/active_record/associations/association.rb +7 -7
  7. data/lib/active_record/associations/association_scope.rb +7 -5
  8. data/lib/active_record/associations/belongs_to_association.rb +7 -3
  9. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +7 -2
  10. data/lib/active_record/associations/builder/association.rb +23 -2
  11. data/lib/active_record/associations/builder/belongs_to.rb +2 -2
  12. data/lib/active_record/associations/has_many_association.rb +1 -1
  13. data/lib/active_record/associations/join_dependency/join_association.rb +8 -7
  14. data/lib/active_record/associations/join_dependency.rb +1 -1
  15. data/lib/active_record/associations.rb +6 -2
  16. data/lib/active_record/attributes.rb +1 -1
  17. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +4 -4
  18. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
  19. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +4 -0
  20. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +7 -1
  21. data/lib/active_record/connection_adapters/abstract_adapter.rb +5 -6
  22. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +6 -10
  23. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +4 -1
  24. data/lib/active_record/connection_adapters/pool_config.rb +13 -3
  25. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +1 -1
  26. data/lib/active_record/connection_adapters/postgresql_adapter.rb +1 -3
  27. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
  28. data/lib/active_record/connection_handling.rb +11 -3
  29. data/lib/active_record/core.rb +42 -24
  30. data/lib/active_record/database_configurations/url_config.rb +1 -1
  31. data/lib/active_record/enum.rb +48 -28
  32. data/lib/active_record/fixtures.rb +4 -1
  33. data/lib/active_record/gem_version.rb +1 -1
  34. data/lib/active_record/locking/optimistic.rb +14 -4
  35. data/lib/active_record/log_subscriber.rb +3 -2
  36. data/lib/active_record/migration/compatibility.rb +2 -1
  37. data/lib/active_record/railties/console_sandbox.rb +2 -4
  38. data/lib/active_record/railties/databases.rake +13 -7
  39. data/lib/active_record/reflection.rb +1 -1
  40. data/lib/active_record/relation/predicate_builder/association_query_value.rb +3 -3
  41. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +9 -5
  42. data/lib/active_record/relation/predicate_builder.rb +2 -5
  43. data/lib/active_record/relation/query_methods.rb +8 -5
  44. data/lib/active_record/signed_id.rb +1 -1
  45. data/lib/active_record/table_metadata.rb +6 -3
  46. data/lib/active_record/tasks/database_tasks.rb +1 -0
  47. data/lib/active_record/transactions.rb +4 -2
  48. metadata +13 -13
@@ -649,9 +649,7 @@ module ActiveRecord
649
649
  raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
650
650
  end
651
651
 
652
- if without_prepared_statement?(binds)
653
- result = exec_no_cache(sql, name, [])
654
- elsif !prepare
652
+ if !prepare || without_prepared_statement?(binds)
655
653
  result = exec_no_cache(sql, name, binds)
656
654
  else
657
655
  result = exec_cache(sql, name, binds)
@@ -271,7 +271,7 @@ module ActiveRecord
271
271
  def change_column(table_name, column_name, type, **options) #:nodoc:
272
272
  alter_table(table_name) do |definition|
273
273
  definition[column_name].instance_eval do
274
- self.type = type
274
+ self.type = aliased_types(type.to_s, type)
275
275
  self.options.merge!(options)
276
276
  end
277
277
  end
@@ -91,6 +91,7 @@ module ActiveRecord
91
91
  db_config, owner_name = resolve_config_for_connection(database_key)
92
92
  handler = lookup_connection_handler(role.to_sym)
93
93
 
94
+ self.connection_class = true
94
95
  connections << handler.establish_connection(db_config, owner_name: owner_name, role: role)
95
96
  end
96
97
 
@@ -99,6 +100,7 @@ module ActiveRecord
99
100
  db_config, owner_name = resolve_config_for_connection(database_key)
100
101
  handler = lookup_connection_handler(role.to_sym)
101
102
 
103
+ self.connection_class = true
102
104
  connections << handler.establish_connection(db_config, owner_name: owner_name, role: role, shard: shard.to_sym)
103
105
  end
104
106
  end
@@ -143,6 +145,10 @@ module ActiveRecord
143
145
  if self != Base && !abstract_class
144
146
  raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
145
147
  end
148
+
149
+ if name != connection_specification_name && !primary_class?
150
+ raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
151
+ end
146
152
  end
147
153
 
148
154
  if database && (role || shard)
@@ -180,12 +186,14 @@ module ActiveRecord
180
186
  #
181
187
  # Usage:
182
188
  #
183
- # ActiveRecord::Base.connected_to(AnimalsRecord, MealsRecord], role: :reading) do
189
+ # ActiveRecord::Base.connected_to_many(AnimalsRecord, MealsRecord, role: :reading) do
184
190
  # Dog.first # Read from animals replica
185
191
  # Dinner.first # Read from meals replica
186
192
  # Person.first # Read from primary writer
187
193
  # end
188
- def connected_to_many(classes, role:, shard: nil, prevent_writes: false)
194
+ def connected_to_many(*classes, role:, shard: nil, prevent_writes: false)
195
+ classes = classes.flatten
196
+
189
197
  if legacy_connection_handling
190
198
  raise NotImplementedError, "connected_to_many is not available with legacy connection handling"
191
199
  end
@@ -357,7 +365,7 @@ module ActiveRecord
357
365
  self.connection_specification_name = owner_name
358
366
 
359
367
  db_config = Base.configurations.resolve(config_or_env)
360
- [db_config, owner_name]
368
+ [db_config, self]
361
369
  end
362
370
 
363
371
  def with_handler(handler_key, &blk)
@@ -196,7 +196,7 @@ module ActiveRecord
196
196
  else
197
197
  connected_to_stack.reverse_each do |hash|
198
198
  return hash[:role] if hash[:role] && hash[:klasses].include?(Base)
199
- return hash[:role] if hash[:role] && hash[:klasses].include?(abstract_base_class)
199
+ return hash[:role] if hash[:role] && hash[:klasses].include?(connection_classes)
200
200
  end
201
201
 
202
202
  default_role
@@ -215,7 +215,7 @@ module ActiveRecord
215
215
  def self.current_shard
216
216
  connected_to_stack.reverse_each do |hash|
217
217
  return hash[:shard] if hash[:shard] && hash[:klasses].include?(Base)
218
- return hash[:shard] if hash[:shard] && hash[:klasses].include?(abstract_base_class)
218
+ return hash[:shard] if hash[:shard] && hash[:klasses].include?(connection_classes)
219
219
  end
220
220
 
221
221
  default_shard
@@ -237,7 +237,7 @@ module ActiveRecord
237
237
  else
238
238
  connected_to_stack.reverse_each do |hash|
239
239
  return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(Base)
240
- return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(abstract_base_class)
240
+ return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(connection_classes)
241
241
  end
242
242
 
243
243
  false
@@ -254,11 +254,23 @@ module ActiveRecord
254
254
  end
255
255
  end
256
256
 
257
- def self.abstract_base_class # :nodoc:
257
+ def self.connection_class=(b) # :nodoc:
258
+ @connection_class = b
259
+ end
260
+
261
+ def self.connection_class # :nodoc
262
+ @connection_class ||= false
263
+ end
264
+
265
+ def self.connection_class? # :nodoc:
266
+ self.connection_class
267
+ end
268
+
269
+ def self.connection_classes # :nodoc:
258
270
  klass = self
259
271
 
260
272
  until klass == Base
261
- break if klass.abstract_class?
273
+ break if klass.connection_class?
262
274
  klass = klass.superclass
263
275
  end
264
276
 
@@ -277,14 +289,14 @@ module ActiveRecord
277
289
  self.default_role = writing_role
278
290
  self.default_shard = :default
279
291
 
280
- def self.strict_loading_violation!(owner:, association:) # :nodoc:
292
+ def self.strict_loading_violation!(owner:, reflection:) # :nodoc:
281
293
  case action_on_strict_loading_violation
282
294
  when :raise
283
- message = "`#{association}` called on `#{owner}` is marked for strict_loading and cannot be lazily loaded."
295
+ message = "`#{owner}` is marked for strict_loading. The `#{reflection.klass}` association named `:#{reflection.name}` cannot be lazily loaded."
284
296
  raise ActiveRecord::StrictLoadingViolationError.new(message)
285
297
  when :log
286
298
  name = "strict_loading_violation.active_record"
287
- ActiveSupport::Notifications.instrument(name, owner: owner, association: association)
299
+ ActiveSupport::Notifications.instrument(name, owner: owner, reflection: reflection)
288
300
  end
289
301
  end
290
302
  end
@@ -332,31 +344,37 @@ module ActiveRecord
332
344
  hash = args.first
333
345
  return super unless Hash === hash
334
346
 
335
- values = hash.values.map! { |value| value.respond_to?(:id) ? value.id : value }
336
- return super if values.any? { |v| StatementCache.unsupported_value?(v) }
337
-
338
- keys = hash.keys.map! do |key|
339
- attribute_aliases[name = key.to_s] || begin
340
- reflection = _reflect_on_association(name)
341
- if reflection&.belongs_to? && !reflection.polymorphic?
342
- reflection.join_foreign_key
343
- elsif reflect_on_aggregation(name)
344
- return super
345
- else
346
- name
347
- end
347
+ hash = hash.each_with_object({}) do |(key, value), h|
348
+ key = key.to_s
349
+ key = attribute_aliases[key] || key
350
+
351
+ return super if reflect_on_aggregation(key)
352
+
353
+ reflection = _reflect_on_association(key)
354
+
355
+ if !reflection
356
+ value = value.id if value.respond_to?(:id)
357
+ elsif reflection.belongs_to? && !reflection.polymorphic?
358
+ key = reflection.join_foreign_key
359
+ pkey = reflection.join_primary_key
360
+ value = value.public_send(pkey) if value.respond_to?(pkey)
348
361
  end
349
- end
350
362
 
351
- return super unless keys.all? { |k| columns_hash.key?(k) }
363
+ if !columns_hash.key?(key) || StatementCache.unsupported_value?(value)
364
+ return super
365
+ end
366
+
367
+ h[key] = value
368
+ end
352
369
 
370
+ keys = hash.keys
353
371
  statement = cached_find_by_statement(keys) { |params|
354
372
  wheres = keys.index_with { params.bind }
355
373
  where(wheres).limit(1)
356
374
  }
357
375
 
358
376
  begin
359
- statement.execute(values, connection).first
377
+ statement.execute(hash.values, connection).first
360
378
  rescue TypeError
361
379
  raise ActiveRecord::StatementInvalid
362
380
  end
@@ -42,7 +42,7 @@ module ActiveRecord
42
42
  # Return a Hash that can be merged into the main config that represents
43
43
  # the passed in url
44
44
  def build_url_hash
45
- if url.nil? || url.start_with?("jdbc:")
45
+ if url.nil? || %w(jdbc: http: https:).any? { |protocol| url.start_with?(protocol) }
46
46
  { url: url }
47
47
  else
48
48
  ConnectionUrlResolver.new(url).to_hash
@@ -158,8 +158,6 @@ module ActiveRecord
158
158
  end
159
159
 
160
160
  def enum(definitions)
161
- klass = self
162
-
163
161
  enum_prefix = definitions.delete(:_prefix)
164
162
  enum_suffix = definitions.delete(:_suffix)
165
163
  enum_scopes = definitions.delete(:_scopes)
@@ -187,55 +185,77 @@ module ActiveRecord
187
185
  EnumType.new(attr, enum_values, subtype)
188
186
  end
189
187
 
188
+ value_method_names = []
190
189
  _enum_methods_module.module_eval do
190
+ prefix = if enum_prefix == true
191
+ "#{name}_"
192
+ elsif enum_prefix
193
+ "#{enum_prefix}_"
194
+ end
195
+
196
+ suffix = if enum_suffix == true
197
+ "_#{name}"
198
+ elsif enum_suffix
199
+ "_#{enum_suffix}"
200
+ end
201
+
191
202
  pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
192
- value_method_names = []
193
203
  pairs.each do |label, value|
194
- if enum_prefix == true
195
- prefix = "#{name}_"
196
- elsif enum_prefix
197
- prefix = "#{enum_prefix}_"
198
- end
199
- if enum_suffix == true
200
- suffix = "_#{name}"
201
- elsif enum_suffix
202
- suffix = "_#{enum_suffix}"
203
- end
204
-
205
- method_friendly_label = label.to_s.gsub(/\W+/, "_")
206
- value_method_name = "#{prefix}#{method_friendly_label}#{suffix}"
207
- value_method_names << value_method_name
208
204
  enum_values[label] = value
209
205
  label = label.to_s
210
206
 
211
- # def active?() status == "active" end
207
+ value_method_name = "#{prefix}#{label}#{suffix}"
208
+ value_method_names << value_method_name
209
+ define_enum_methods(name, value_method_name, value, enum_scopes)
210
+
211
+ method_friendly_label = label.gsub(/[\W&&[:ascii:]]+/, "_")
212
+ value_method_alias = "#{prefix}#{method_friendly_label}#{suffix}"
213
+
214
+ if value_method_alias != value_method_name && !value_method_names.include?(value_method_alias)
215
+ value_method_names << value_method_alias
216
+ define_enum_methods(name, value_method_alias, value, enum_scopes)
217
+ end
218
+ end
219
+ end
220
+ detect_negative_enum_conditions!(value_method_names) if enum_scopes != false
221
+ enum_values.freeze
222
+ end
223
+ end
224
+
225
+ private
226
+ class EnumMethods < Module # :nodoc:
227
+ def initialize(klass)
228
+ @klass = klass
229
+ end
230
+
231
+ private
232
+ attr_reader :klass
233
+
234
+ def define_enum_methods(name, value_method_name, value, enum_scopes)
235
+ # def active?() status_for_database == 0 end
212
236
  klass.send(:detect_enum_conflict!, name, "#{value_method_name}?")
213
- define_method("#{value_method_name}?") { self[attr] == label }
237
+ define_method("#{value_method_name}?") { public_send(:"#{name}_for_database") == value }
214
238
 
215
239
  # def active!() update!(status: 0) end
216
240
  klass.send(:detect_enum_conflict!, name, "#{value_method_name}!")
217
- define_method("#{value_method_name}!") { update!(attr => value) }
241
+ define_method("#{value_method_name}!") { update!(name => value) }
218
242
 
219
243
  # scope :active, -> { where(status: 0) }
220
244
  # scope :not_active, -> { where.not(status: 0) }
221
245
  if enum_scopes != false
222
246
  klass.send(:detect_enum_conflict!, name, value_method_name, true)
223
- klass.scope value_method_name, -> { where(attr => value) }
247
+ klass.scope value_method_name, -> { where(name => value) }
224
248
 
225
249
  klass.send(:detect_enum_conflict!, name, "not_#{value_method_name}", true)
226
- klass.scope "not_#{value_method_name}", -> { where.not(attr => value) }
250
+ klass.scope "not_#{value_method_name}", -> { where.not(name => value) }
227
251
  end
228
252
  end
229
- klass.send(:detect_negative_enum_conditions!, value_method_names) if enum_scopes != false
230
- end
231
- enum_values.freeze
232
253
  end
233
- end
254
+ private_constant :EnumMethods
234
255
 
235
- private
236
256
  def _enum_methods_module
237
257
  @_enum_methods_module ||= begin
238
- mod = Module.new
258
+ mod = EnumMethods.new(self)
239
259
  include mod
240
260
  mod
241
261
  end
@@ -773,9 +773,12 @@ module ActiveRecord
773
773
 
774
774
  def find
775
775
  raise FixtureClassNotFound, "No class attached to find." unless model_class
776
- model_class.unscoped do
776
+ object = model_class.unscoped do
777
777
  model_class.find(fixture[model_class.primary_key])
778
778
  end
779
+ # Fixtures can't be eagerly loaded
780
+ object.instance_variable_set(:@strict_loading, false)
781
+ object
779
782
  end
780
783
  end
781
784
  end
@@ -9,7 +9,7 @@ module ActiveRecord
9
9
  module VERSION
10
10
  MAJOR = 6
11
11
  MINOR = 1
12
- TINY = 0
12
+ TINY = 2
13
13
  PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
@@ -89,7 +89,9 @@ module ActiveRecord
89
89
 
90
90
  begin
91
91
  locking_column = self.class.locking_column
92
- previous_lock_value = attribute_before_type_cast(locking_column)
92
+ lock_attribute_was = @attributes[locking_column]
93
+ lock_value_for_database = _lock_value_for_database(locking_column)
94
+
93
95
  attribute_names = attribute_names.dup if attribute_names.frozen?
94
96
  attribute_names << locking_column
95
97
 
@@ -98,7 +100,7 @@ module ActiveRecord
98
100
  affected_rows = self.class._update_record(
99
101
  attributes_with_values(attribute_names),
100
102
  @primary_key => id_in_database,
101
- locking_column => @attributes[locking_column].original_value_for_database
103
+ locking_column => lock_value_for_database
102
104
  )
103
105
 
104
106
  if affected_rows != 1
@@ -109,7 +111,7 @@ module ActiveRecord
109
111
 
110
112
  # If something went wrong, revert the locking_column value.
111
113
  rescue Exception
112
- self[locking_column] = previous_lock_value.to_i
114
+ @attributes[locking_column] = lock_attribute_was
113
115
  raise
114
116
  end
115
117
  end
@@ -121,7 +123,7 @@ module ActiveRecord
121
123
 
122
124
  affected_rows = self.class._delete_record(
123
125
  @primary_key => id_in_database,
124
- locking_column => attribute_before_type_cast(locking_column)
126
+ locking_column => _lock_value_for_database(locking_column)
125
127
  )
126
128
 
127
129
  if affected_rows != 1
@@ -131,6 +133,14 @@ module ActiveRecord
131
133
  affected_rows
132
134
  end
133
135
 
136
+ def _lock_value_for_database(locking_column)
137
+ if will_save_change_to_attribute?(locking_column)
138
+ @attributes[locking_column].value_for_database
139
+ else
140
+ @attributes[locking_column].original_value_for_database
141
+ end
142
+ end
143
+
134
144
  module ClassMethods
135
145
  DEFAULT_LOCKING_COLUMN = "lock_version"
136
146
 
@@ -22,9 +22,10 @@ module ActiveRecord
22
22
  def strict_loading_violation(event)
23
23
  debug do
24
24
  owner = event.payload[:owner]
25
- association = event.payload[:association]
25
+ association = event.payload[:reflection].klass
26
+ name = event.payload[:reflection].name
26
27
 
27
- color("Strict loading violation: #{association} lazily loaded on #{owner}.", RED)
28
+ color("Strict loading violation: #{owner} is marked for strict loading. The #{association} association named :#{name} cannot be lazily loaded.", RED)
28
29
  end
29
30
  end
30
31
 
@@ -56,7 +56,8 @@ module ActiveRecord
56
56
  end
57
57
 
58
58
  def add_reference(table_name, ref_name, **options)
59
- ReferenceDefinition.new(ref_name, **options).add_to(update_table_definition(table_name, self))
59
+ ReferenceDefinition.new(ref_name, **options)
60
+ .add_to(connection.update_table_definition(table_name, self))
60
61
  end
61
62
  alias :add_belongs_to :add_reference
62
63
 
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- ActiveRecord::Base.connection.begin_transaction(joinable: false)
4
-
5
- at_exit do
6
- ActiveRecord::Base.connection.rollback_transaction
3
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.set_callback(:checkout, :after) do
4
+ begin_transaction(joinable: false)
7
5
  end
@@ -361,17 +361,23 @@ db_namespace = namespace :db do
361
361
 
362
362
  # Skipped when no database
363
363
  ActiveRecord::Tasks::DatabaseTasks.migrate
364
+
364
365
  if ActiveRecord::Base.dump_schema_after_migration
365
366
  ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, ActiveRecord::Base.schema_format)
366
367
  end
367
-
368
368
  rescue ActiveRecord::NoDatabaseError
369
- ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, db_config.name)
370
- ActiveRecord::Tasks::DatabaseTasks.load_schema(
371
- db_config,
372
- ActiveRecord::Base.schema_format,
373
- nil
374
- )
369
+ config_name = db_config.name
370
+ ActiveRecord::Tasks::DatabaseTasks.create_current(db_config.env_name, config_name)
371
+
372
+ if File.exist?(ActiveRecord::Tasks::DatabaseTasks.dump_filename(config_name))
373
+ ActiveRecord::Tasks::DatabaseTasks.load_schema(
374
+ db_config,
375
+ ActiveRecord::Base.schema_format,
376
+ nil
377
+ )
378
+ else
379
+ ActiveRecord::Tasks::DatabaseTasks.migrate
380
+ end
375
381
 
376
382
  seed = true
377
383
  end
@@ -162,7 +162,7 @@ module ActiveRecord
162
162
  # <tt>composed_of :balance, class_name: 'Money'</tt> returns <tt>'Money'</tt>
163
163
  # <tt>has_many :clients</tt> returns <tt>'Client'</tt>
164
164
  def class_name
165
- @class_name ||= -(options[:class_name]&.to_s || derive_class_name)
165
+ @class_name ||= -(options[:class_name] || derive_class_name).to_s
166
166
  end
167
167
 
168
168
  # Returns a list of scopes that should be applied for this Reflection
@@ -9,7 +9,7 @@ module ActiveRecord
9
9
  end
10
10
 
11
11
  def queries
12
- [associated_table.join_foreign_key => ids]
12
+ [ associated_table.join_foreign_key => ids ]
13
13
  end
14
14
 
15
15
  private
@@ -31,8 +31,8 @@ module ActiveRecord
31
31
  end
32
32
 
33
33
  def convert_to_id(value)
34
- if value.respond_to?(:id)
35
- value.id
34
+ if value.respond_to?(primary_key)
35
+ value.public_send(primary_key)
36
36
  else
37
37
  value
38
38
  end
@@ -9,11 +9,13 @@ module ActiveRecord
9
9
  end
10
10
 
11
11
  def queries
12
+ return [ associated_table.join_foreign_key => values ] if values.empty?
13
+
12
14
  type_to_ids_mapping.map do |type, ids|
13
- {
14
- associated_table.join_foreign_type => type,
15
- associated_table.join_foreign_key => ids
16
- }
15
+ query = {}
16
+ query[associated_table.join_foreign_type] = type if type
17
+ query[associated_table.join_foreign_key] = ids
18
+ query
17
19
  end
18
20
  end
19
21
 
@@ -23,7 +25,7 @@ module ActiveRecord
23
25
  def type_to_ids_mapping
24
26
  default_hash = Hash.new { |hsh, key| hsh[key] = [] }
25
27
  values.each_with_object(default_hash) do |value, hash|
26
- hash[klass(value).polymorphic_name] << convert_to_id(value)
28
+ hash[klass(value)&.polymorphic_name] << convert_to_id(value)
27
29
  end
28
30
  end
29
31
 
@@ -46,6 +48,8 @@ module ActiveRecord
46
48
  value._read_attribute(primary_key(value))
47
49
  when Relation
48
50
  value.select(primary_key(value))
51
+ else
52
+ value
49
53
  end
50
54
  end
51
55
  end
@@ -93,11 +93,8 @@ module ActiveRecord
93
93
  # PriceEstimate.where(estimate_of: treasure)
94
94
  associated_table = table.associated_table(key)
95
95
  if associated_table.polymorphic_association?
96
- case value.is_a?(Array) ? value.first : value
97
- when Base, Relation
98
- value = [value] unless value.is_a?(Array)
99
- klass = PolymorphicArrayValue
100
- end
96
+ value = [value] unless value.is_a?(Array)
97
+ klass = PolymorphicArrayValue
101
98
  elsif associated_table.through_association?
102
99
  next associated_table.predicate_builder.expand_from_hash(
103
100
  associated_table.primary_key => value
@@ -1081,12 +1081,15 @@ module ActiveRecord
1081
1081
  when String, Array
1082
1082
  parts = [klass.sanitize_sql(rest.empty? ? opts : [opts, *rest])]
1083
1083
  when Hash
1084
- opts = opts.stringify_keys
1084
+ opts = opts.transform_keys do |key|
1085
+ key = key.to_s
1086
+ klass.attribute_aliases[key] || key
1087
+ end
1085
1088
  references = PredicateBuilder.references(opts)
1086
1089
  self.references_values |= references unless references.empty?
1087
1090
 
1088
1091
  parts = predicate_builder.build_from_hash(opts) do |table_name|
1089
- lookup_reflection_from_join_dependencies(table_name)
1092
+ lookup_table_klass_from_join_dependencies(table_name)
1090
1093
  end
1091
1094
  when Arel::Nodes::Node
1092
1095
  parts = [opts]
@@ -1099,9 +1102,9 @@ module ActiveRecord
1099
1102
  alias :build_having_clause :build_where_clause
1100
1103
 
1101
1104
  private
1102
- def lookup_reflection_from_join_dependencies(table_name)
1105
+ def lookup_table_klass_from_join_dependencies(table_name)
1103
1106
  each_join_dependencies do |join|
1104
- return join.reflection if table_name == join.table_name
1107
+ return join.base_klass if table_name == join.table_name
1105
1108
  end
1106
1109
  nil
1107
1110
  end
@@ -1312,7 +1315,7 @@ module ActiveRecord
1312
1315
  elsif field.match?(/\A\w+\.\w+\z/)
1313
1316
  table, column = field.split(".")
1314
1317
  predicate_builder.resolve_arel_attribute(table, column) do
1315
- lookup_reflection_from_join_dependencies(table)
1318
+ lookup_table_klass_from_join_dependencies(table)
1316
1319
  end
1317
1320
  else
1318
1321
  yield field
@@ -20,7 +20,7 @@ module ActiveRecord
20
20
  # a certain time period.
21
21
  #
22
22
  # You set the time period that the signed id is valid for during generation, using the instance method
23
- # +signed_id(expires_in: 15.minutes)+. If the time has elapsed before a signed find is attempted,
23
+ # <tt>signed_id(expires_in: 15.minutes)</tt>. If the time has elapsed before a signed find is attempted,
24
24
  # the signed id will no longer be valid, and nil is returned.
25
25
  #
26
26
  # It's possible to further restrict the use of a signed id with a purpose. This helps when you have a
@@ -33,10 +33,13 @@ module ActiveRecord
33
33
  return self
34
34
  end
35
35
 
36
- reflection ||= yield table_name if block_given?
36
+ if reflection
37
+ association_klass = reflection.klass unless reflection.polymorphic?
38
+ elsif block_given?
39
+ association_klass = yield table_name
40
+ end
37
41
 
38
- if reflection && !reflection.polymorphic?
39
- association_klass = reflection.klass
42
+ if association_klass
40
43
  arel_table = association_klass.arel_table
41
44
  arel_table = arel_table.alias(table_name) if arel_table.name != table_name
42
45
  TableMetadata.new(association_klass, arel_table, reflection)
@@ -380,6 +380,7 @@ module ActiveRecord
380
380
  filename = dump_filename(db_config.name, format)
381
381
  connection = ActiveRecord::Base.connection
382
382
 
383
+ FileUtils.mkdir_p(db_dir)
383
384
  case format
384
385
  when :ruby
385
386
  File.open(filename, "w:utf-8") do |file|
@@ -271,8 +271,10 @@ module ActiveRecord
271
271
  if options[:on]
272
272
  fire_on = Array(options[:on])
273
273
  assert_valid_transaction_action(fire_on)
274
- options[:if] = Array(options[:if])
275
- options[:if].unshift(-> { transaction_include_any_action?(fire_on) })
274
+ options[:if] = [
275
+ -> { transaction_include_any_action?(fire_on) },
276
+ *options[:if]
277
+ ]
276
278
  end
277
279
  end
278
280