activerecord 4.2.0 → 4.2.11

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 (110) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +657 -1
  3. data/lib/active_record.rb +3 -0
  4. data/lib/active_record/aggregations.rb +6 -3
  5. data/lib/active_record/association_relation.rb +13 -0
  6. data/lib/active_record/associations.rb +5 -4
  7. data/lib/active_record/associations/association.rb +15 -3
  8. data/lib/active_record/associations/association_scope.rb +1 -0
  9. data/lib/active_record/associations/belongs_to_association.rb +13 -5
  10. data/lib/active_record/associations/builder/association.rb +1 -1
  11. data/lib/active_record/associations/builder/collection_association.rb +5 -1
  12. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +8 -4
  13. data/lib/active_record/associations/collection_association.rb +35 -15
  14. data/lib/active_record/associations/collection_proxy.rb +15 -9
  15. data/lib/active_record/associations/foreign_association.rb +11 -0
  16. data/lib/active_record/associations/has_many_association.rb +30 -15
  17. data/lib/active_record/associations/has_many_through_association.rb +11 -2
  18. data/lib/active_record/associations/has_one_association.rb +1 -0
  19. data/lib/active_record/associations/join_dependency.rb +8 -2
  20. data/lib/active_record/associations/join_dependency/join_association.rb +7 -1
  21. data/lib/active_record/associations/preloader.rb +4 -4
  22. data/lib/active_record/associations/preloader/association.rb +5 -1
  23. data/lib/active_record/associations/singular_association.rb +2 -8
  24. data/lib/active_record/associations/through_association.rb +11 -6
  25. data/lib/active_record/attribute.rb +15 -1
  26. data/lib/active_record/attribute_assignment.rb +2 -2
  27. data/lib/active_record/attribute_methods.rb +4 -8
  28. data/lib/active_record/attribute_methods/before_type_cast.rb +5 -0
  29. data/lib/active_record/attribute_methods/dirty.rb +14 -4
  30. data/lib/active_record/attribute_methods/time_zone_conversion.rb +5 -1
  31. data/lib/active_record/attribute_methods/write.rb +1 -1
  32. data/lib/active_record/attribute_set.rb +4 -0
  33. data/lib/active_record/attribute_set/builder.rb +32 -12
  34. data/lib/active_record/attributes.rb +8 -0
  35. data/lib/active_record/autosave_association.rb +24 -9
  36. data/lib/active_record/base.rb +4 -5
  37. data/lib/active_record/callbacks.rb +1 -1
  38. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +12 -6
  39. data/lib/active_record/connection_adapters/abstract/database_statements.rb +23 -3
  40. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
  41. data/lib/active_record/connection_adapters/abstract/quoting.rb +1 -0
  42. data/lib/active_record/connection_adapters/abstract/savepoints.rb +1 -1
  43. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +26 -16
  44. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +87 -24
  45. data/lib/active_record/connection_adapters/abstract/transaction.rb +2 -6
  46. data/lib/active_record/connection_adapters/abstract_adapter.rb +25 -7
  47. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +73 -10
  48. data/lib/active_record/connection_adapters/column.rb +2 -2
  49. data/lib/active_record/connection_adapters/mysql2_adapter.rb +7 -21
  50. data/lib/active_record/connection_adapters/mysql_adapter.rb +10 -3
  51. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +1 -1
  52. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +2 -1
  53. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +1 -0
  54. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +9 -0
  55. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +3 -1
  56. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +1 -1
  57. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -0
  58. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +17 -5
  59. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +3 -3
  60. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +21 -13
  61. data/lib/active_record/connection_adapters/postgresql_adapter.rb +12 -12
  62. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +12 -28
  63. data/lib/active_record/connection_handling.rb +1 -1
  64. data/lib/active_record/core.rb +28 -15
  65. data/lib/active_record/counter_cache.rb +1 -1
  66. data/lib/active_record/enum.rb +2 -3
  67. data/lib/active_record/errors.rb +6 -5
  68. data/lib/active_record/explain_subscriber.rb +1 -1
  69. data/lib/active_record/fixtures.rb +9 -7
  70. data/lib/active_record/gem_version.rb +1 -1
  71. data/lib/active_record/legacy_yaml_adapter.rb +30 -0
  72. data/lib/active_record/locking/optimistic.rb +16 -14
  73. data/lib/active_record/migration.rb +38 -10
  74. data/lib/active_record/model_schema.rb +4 -2
  75. data/lib/active_record/nested_attributes.rb +13 -3
  76. data/lib/active_record/no_touching.rb +1 -1
  77. data/lib/active_record/persistence.rb +7 -4
  78. data/lib/active_record/railtie.rb +5 -3
  79. data/lib/active_record/railties/databases.rake +17 -24
  80. data/lib/active_record/reflection.rb +40 -28
  81. data/lib/active_record/relation.rb +3 -2
  82. data/lib/active_record/relation/calculations.rb +10 -3
  83. data/lib/active_record/relation/delegation.rb +1 -1
  84. data/lib/active_record/relation/finder_methods.rb +4 -16
  85. data/lib/active_record/relation/merger.rb +24 -1
  86. data/lib/active_record/relation/predicate_builder.rb +32 -3
  87. data/lib/active_record/relation/predicate_builder/array_handler.rb +3 -2
  88. data/lib/active_record/relation/query_methods.rb +29 -27
  89. data/lib/active_record/relation/spawn_methods.rb +7 -3
  90. data/lib/active_record/schema_dumper.rb +1 -1
  91. data/lib/active_record/schema_migration.rb +1 -4
  92. data/lib/active_record/scoping/default.rb +1 -0
  93. data/lib/active_record/tasks/database_tasks.rb +5 -2
  94. data/lib/active_record/tasks/mysql_database_tasks.rb +30 -16
  95. data/lib/active_record/tasks/postgresql_database_tasks.rb +19 -8
  96. data/lib/active_record/transactions.rb +21 -11
  97. data/lib/active_record/type/boolean.rb +1 -0
  98. data/lib/active_record/type/date.rb +4 -0
  99. data/lib/active_record/type/date_time.rb +14 -3
  100. data/lib/active_record/type/decimal.rb +27 -3
  101. data/lib/active_record/type/hash_lookup_type_map.rb +8 -2
  102. data/lib/active_record/type/integer.rb +9 -5
  103. data/lib/active_record/type/numeric.rb +1 -1
  104. data/lib/active_record/type/serialized.rb +7 -1
  105. data/lib/active_record/type/string.rb +4 -0
  106. data/lib/active_record/type/value.rb +9 -0
  107. data/lib/active_record/validations/uniqueness.rb +16 -6
  108. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +0 -3
  109. data/lib/rails/generators/active_record/migration/templates/migration.rb +0 -6
  110. metadata +9 -7
@@ -167,7 +167,7 @@ module ActiveRecord
167
167
 
168
168
  def each_counter_cached_associations
169
169
  _reflections.each do |name, reflection|
170
- yield association(name) if reflection.belongs_to? && reflection.counter_cache_column
170
+ yield association(name.to_sym) if reflection.belongs_to? && reflection.counter_cache_column
171
171
  end
172
172
  end
173
173
 
@@ -18,10 +18,9 @@ module ActiveRecord
18
18
  # conversation.archived? # => true
19
19
  # conversation.status # => "archived"
20
20
  #
21
- # # conversation.update! status: 1
21
+ # # conversation.status = 1
22
22
  # conversation.status = "archived"
23
23
  #
24
- # # conversation.update! status: nil
25
24
  # conversation.status = nil
26
25
  # conversation.status.nil? # => true
27
26
  # conversation.status # => nil
@@ -70,7 +69,7 @@ module ActiveRecord
70
69
  # Where conditions on an enum attribute must use the ordinal value of an enum.
71
70
  module Enum
72
71
  def self.extended(base) # :nodoc:
73
- base.class_attribute(:defined_enums)
72
+ base.class_attribute(:defined_enums, instance_writer: false)
74
73
  base.defined_enums = {}
75
74
  end
76
75
 
@@ -71,9 +71,9 @@ module ActiveRecord
71
71
  class RecordNotDestroyed < ActiveRecordError
72
72
  attr_reader :record
73
73
 
74
- def initialize(record)
74
+ def initialize(message, record = nil)
75
75
  @record = record
76
- super()
76
+ super(message)
77
77
  end
78
78
  end
79
79
 
@@ -219,11 +219,12 @@ module ActiveRecord
219
219
  class UnknownPrimaryKey < ActiveRecordError
220
220
  attr_reader :model
221
221
 
222
- def initialize(model)
223
- super("Unknown primary key for table #{model.table_name} in model #{model}.")
222
+ def initialize(model, description = nil)
223
+ message = "Unknown primary key for table #{model.table_name} in model #{model}."
224
+ message += "\n#{description}" if description
225
+ super(message)
224
226
  @model = model
225
227
  end
226
-
227
228
  end
228
229
 
229
230
  # Raised when a relation cannot be mutated because it's already loaded.
@@ -19,7 +19,7 @@ module ActiveRecord
19
19
  # On the other hand, we want to monitor the performance of our real database
20
20
  # queries, not the performance of the access to the query cache.
21
21
  IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE)
22
- EXPLAINED_SQLS = /\A\s*(select|update|delete|insert)\b/i
22
+ EXPLAINED_SQLS = /\A\s*(with|select|update|delete|insert)\b/i
23
23
  def ignore_payload?(payload)
24
24
  payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS
25
25
  end
@@ -534,12 +534,10 @@ module ActiveRecord
534
534
  conn.insert_fixture(row, fixture_set_name)
535
535
  end
536
536
  end
537
- end
538
537
 
539
- # Cap primary key sequences to max(pk).
540
- if connection.respond_to?(:reset_pk_sequence!)
541
- fixture_sets.each do |fs|
542
- connection.reset_pk_sequence!(fs.table_name)
538
+ # Cap primary key sequences to max(pk).
539
+ if conn.respond_to?(:reset_pk_sequence!)
540
+ conn.reset_pk_sequence!(fs.table_name)
543
541
  end
544
542
  end
545
543
  end
@@ -633,7 +631,7 @@ module ActiveRecord
633
631
 
634
632
  # interpolate the fixture label
635
633
  row.each do |key, value|
636
- row[key] = value.gsub("$LABEL", label) if value.is_a?(String)
634
+ row[key] = value.gsub("$LABEL", label.to_s) if value.is_a?(String)
637
635
  end
638
636
 
639
637
  # generate a primary key if necessary
@@ -661,7 +659,7 @@ module ActiveRecord
661
659
  row[association.foreign_type] = $1
662
660
  end
663
661
 
664
- fk_type = association.active_record.columns_hash[fk_name].type
662
+ fk_type = reflection_class.columns_hash[fk_name].type
665
663
  row[fk_name] = ActiveRecord::FixtureSet.identify(value, fk_type)
666
664
  end
667
665
  when :has_many
@@ -703,6 +701,10 @@ module ActiveRecord
703
701
  def lhs_key
704
702
  @association.through_reflection.foreign_key
705
703
  end
704
+
705
+ def join_table
706
+ @association.through_reflection.table_name
707
+ end
706
708
  end
707
709
 
708
710
  private
@@ -7,7 +7,7 @@ module ActiveRecord
7
7
  module VERSION
8
8
  MAJOR = 4
9
9
  MINOR = 2
10
- TINY = 0
10
+ TINY = 11
11
11
  PRE = nil
12
12
 
13
13
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
@@ -0,0 +1,30 @@
1
+ module ActiveRecord
2
+ module LegacyYamlAdapter
3
+ def self.convert(klass, coder)
4
+ return coder unless coder.is_a?(Psych::Coder)
5
+
6
+ case coder["active_record_yaml_version"]
7
+ when 0 then coder
8
+ else
9
+ if coder["attributes"].is_a?(AttributeSet)
10
+ coder
11
+ else
12
+ Rails41.convert(klass, coder)
13
+ end
14
+ end
15
+ end
16
+
17
+ module Rails41
18
+ def self.convert(klass, coder)
19
+ attributes = klass.attributes_builder
20
+ .build_from_database(coder["attributes"])
21
+ new_record = coder["attributes"][klass.primary_key].blank?
22
+
23
+ {
24
+ "attributes" => attributes,
25
+ "new_record" => new_record,
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -66,6 +66,15 @@ module ActiveRecord
66
66
  send(lock_col + '=', previous_lock_value + 1)
67
67
  end
68
68
 
69
+ def _create_record(attribute_names = self.attribute_names, *) # :nodoc:
70
+ if locking_enabled?
71
+ # We always want to persist the locking version, even if we don't detect
72
+ # a change from the default, since the database might have no default
73
+ attribute_names |= [self.class.locking_column]
74
+ end
75
+ super
76
+ end
77
+
69
78
  def _update_record(attribute_names = self.attribute_names) #:nodoc:
70
79
  return super unless locking_enabled?
71
80
  return 0 if attribute_names.empty?
@@ -80,17 +89,15 @@ module ActiveRecord
80
89
  begin
81
90
  relation = self.class.unscoped
82
91
 
83
- stmt = relation.where(
84
- relation.table[self.class.primary_key].eq(id).and(
85
- relation.table[lock_col].eq(self.class.quote_value(previous_lock_value, column_for_attribute(lock_col)))
86
- )
87
- ).arel.compile_update(
88
- arel_attributes_with_values_for_update(attribute_names),
89
- self.class.primary_key
92
+ affected_rows = relation.where(
93
+ self.class.primary_key => id,
94
+ lock_col => previous_lock_value,
95
+ ).update_all(
96
+ Hash[attributes_for_update(attribute_names).map do |name|
97
+ [name, _read_attribute(name)]
98
+ end]
90
99
  )
91
100
 
92
- affected_rows = self.class.connection.update stmt
93
-
94
101
  unless affected_rows == 1
95
102
  raise ActiveRecord::StaleObjectError.new(self, "update")
96
103
  end
@@ -187,11 +194,6 @@ module ActiveRecord
187
194
  super.to_i
188
195
  end
189
196
 
190
- def changed?(old_value, *)
191
- # Ensure we save if the default was `nil`
192
- super || old_value == 0
193
- end
194
-
195
197
  def init_with(coder)
196
198
  __setobj__(coder['subtype'])
197
199
  end
@@ -39,7 +39,7 @@ module ActiveRecord
39
39
 
40
40
  class PendingMigrationError < MigrationError#:nodoc:
41
41
  def initialize
42
- if defined?(Rails)
42
+ if defined?(Rails.env)
43
43
  super("Migrations are pending. To resolve this issue, run:\n\n\tbin/rake db:migrate RAILS_ENV=#{::Rails.env}")
44
44
  else
45
45
  super("Migrations are pending. To resolve this issue, run:\n\n\tbin/rake db:migrate")
@@ -307,9 +307,8 @@ module ActiveRecord
307
307
  #
308
308
  # == Reversible Migrations
309
309
  #
310
- # Starting with Rails 3.1, you will be able to define reversible migrations.
311
310
  # Reversible migrations are migrations that know how to go +down+ for you.
312
- # You simply supply the +up+ logic, and the Migration system will figure out
311
+ # You simply supply the +up+ logic, and the Migration system figures out
313
312
  # how to execute the down commands for you.
314
313
  #
315
314
  # To define a reversible migration, define the +change+ method in your
@@ -421,7 +420,10 @@ module ActiveRecord
421
420
  new.migrate direction
422
421
  end
423
422
 
424
- # Disable DDL transactions for this migration.
423
+ # Disable the transaction wrapping this migration.
424
+ # You can still create your own transactions even after calling #disable_ddl_transaction!
425
+ #
426
+ # For more details read the {"Transactional Migrations" section above}[rdoc-ref:Migration].
425
427
  def disable_ddl_transaction!
426
428
  @disable_ddl_transaction = true
427
429
  end
@@ -653,7 +655,8 @@ module ActiveRecord
653
655
  unless @connection.respond_to? :revert
654
656
  unless arguments.empty? || [:execute, :enable_extension, :disable_extension].include?(method)
655
657
  arguments[0] = proper_table_name(arguments.first, table_name_options)
656
- if [:rename_table, :add_foreign_key].include?(method)
658
+ if [:rename_table, :add_foreign_key].include?(method) ||
659
+ (method == :remove_foreign_key && !arguments.second.is_a?(Hash))
657
660
  arguments[1] = proper_table_name(arguments.second, table_name_options)
658
661
  end
659
662
  end
@@ -877,14 +880,15 @@ module ActiveRecord
877
880
  migrations_paths.first
878
881
  end
879
882
 
883
+ def parse_migration_filename(filename) # :nodoc:
884
+ File.basename(filename).scan(/\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first
885
+ end
886
+
880
887
  def migrations(paths)
881
888
  paths = Array(paths)
882
889
 
883
- files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }]
884
-
885
- migrations = files.map do |file|
886
- version, name, scope = file.scan(/([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/).first
887
-
890
+ migrations = migration_files(paths).map do |file|
891
+ version, name, scope = parse_migration_filename(file)
888
892
  raise IllegalMigrationNameError.new(file) unless version
889
893
  version = version.to_i
890
894
  name = name.camelize
@@ -895,6 +899,30 @@ module ActiveRecord
895
899
  migrations.sort_by(&:version)
896
900
  end
897
901
 
902
+ def migrations_status(paths)
903
+ paths = Array(paths)
904
+
905
+ db_list = ActiveRecord::SchemaMigration.normalized_versions
906
+
907
+ file_list = migration_files(paths).map do |file|
908
+ version, name, scope = parse_migration_filename(file)
909
+ raise IllegalMigrationNameError.new(file) unless version
910
+ version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
911
+ status = db_list.delete(version) ? "up" : "down"
912
+ [status, version, (name + scope).humanize]
913
+ end.compact
914
+
915
+ db_list.map! do |version|
916
+ ["up", version, "********** NO FILE **********"]
917
+ end
918
+
919
+ (db_list + file_list).sort_by { |_, version, _| version }
920
+ end
921
+
922
+ def migration_files(paths)
923
+ Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
924
+ end
925
+
898
926
  private
899
927
 
900
928
  def move(direction, migrations_paths, steps)
@@ -247,7 +247,7 @@ module ActiveRecord
247
247
  # Returns a hash where the keys are column names and the values are
248
248
  # default values when instantiating the AR object for this table.
249
249
  def column_defaults
250
- _default_attributes.to_hash
250
+ _default_attributes.dup.to_hash
251
251
  end
252
252
 
253
253
  def _default_attributes # :nodoc:
@@ -295,7 +295,7 @@ module ActiveRecord
295
295
  def reset_column_information
296
296
  connection.clear_cache!
297
297
  undefine_attribute_methods
298
- connection.schema_cache.clear_table_cache!(table_name) if table_exists?
298
+ connection.schema_cache.clear_table_cache!(table_name)
299
299
 
300
300
  @arel_engine = nil
301
301
  @column_names = nil
@@ -304,6 +304,8 @@ module ActiveRecord
304
304
  @default_attributes = nil
305
305
  @inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
306
306
  @relation = nil
307
+
308
+ initialize_find_by_cache
307
309
  end
308
310
 
309
311
  private
@@ -307,7 +307,7 @@ module ActiveRecord
307
307
  attr_names.each do |association_name|
308
308
  if reflection = _reflect_on_association(association_name)
309
309
  reflection.autosave = true
310
- add_autosave_association_callbacks(reflection)
310
+ define_autosave_validation_callbacks(reflection)
311
311
 
312
312
  nested_attributes_options = self.nested_attributes_options.dup
313
313
  nested_attributes_options[association_name.to_sym] = options
@@ -523,7 +523,7 @@ module ActiveRecord
523
523
  # has_destroy_flag? or if a <tt>:reject_if</tt> proc exists for this
524
524
  # association and evaluates to +true+.
525
525
  def reject_new_record?(association_name, attributes)
526
- has_destroy_flag?(attributes) || call_reject_if(association_name, attributes)
526
+ will_be_destroyed?(association_name, attributes) || call_reject_if(association_name, attributes)
527
527
  end
528
528
 
529
529
  # Determines if a record with the particular +attributes+ should be
@@ -532,7 +532,8 @@ module ActiveRecord
532
532
  #
533
533
  # Returns false if there is a +destroy_flag+ on the attributes.
534
534
  def call_reject_if(association_name, attributes)
535
- return false if has_destroy_flag?(attributes)
535
+ return false if will_be_destroyed?(association_name, attributes)
536
+
536
537
  case callback = self.nested_attributes_options[association_name][:reject_if]
537
538
  when Symbol
538
539
  method(callback).arity == 0 ? send(callback) : send(callback, attributes)
@@ -541,6 +542,15 @@ module ActiveRecord
541
542
  end
542
543
  end
543
544
 
545
+ # Only take into account the destroy flag if <tt>:allow_destroy</tt> is true
546
+ def will_be_destroyed?(association_name, attributes)
547
+ allow_destroy?(association_name) && has_destroy_flag?(attributes)
548
+ end
549
+
550
+ def allow_destroy?(association_name)
551
+ self.nested_attributes_options[association_name][:allow_destroy]
552
+ end
553
+
544
554
  def raise_nested_attributes_record_not_found!(association_name, record_id)
545
555
  raise RecordNotFound, "Couldn't find #{self.class._reflect_on_association(association_name).klass.name} with ID=#{record_id} for #{self.class.name} with ID=#{id}"
546
556
  end
@@ -45,7 +45,7 @@ module ActiveRecord
45
45
  NoTouching.applied_to?(self.class)
46
46
  end
47
47
 
48
- def touch(*)
48
+ def touch(*) # :nodoc:
49
49
  super unless no_touching?
50
50
  end
51
51
  end
@@ -139,7 +139,7 @@ module ActiveRecord
139
139
  # Attributes marked as readonly are silently ignored if the record is
140
140
  # being updated.
141
141
  def save!(*)
142
- create_or_update || raise(RecordNotSaved.new(nil, self))
142
+ create_or_update || raise(RecordNotSaved.new("Failed to save the record", self))
143
143
  end
144
144
 
145
145
  # Deletes the record in the database and freezes this instance to
@@ -168,6 +168,7 @@ module ActiveRecord
168
168
  def destroy
169
169
  raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
170
170
  destroy_associations
171
+ self.class.connection.add_transaction_record(self)
171
172
  destroy_row if persisted?
172
173
  @destroyed = true
173
174
  freeze
@@ -181,7 +182,7 @@ module ActiveRecord
181
182
  # and <tt>destroy!</tt> raises ActiveRecord::RecordNotDestroyed. See
182
183
  # ActiveRecord::Callbacks for further details.
183
184
  def destroy!
184
- destroy || raise(ActiveRecord::RecordNotDestroyed, self)
185
+ destroy || raise(RecordNotDestroyed.new("Failed to destroy the record", self))
185
186
  end
186
187
 
187
188
  # Returns an instance of the specified +klass+ with the attributes of the
@@ -197,7 +198,8 @@ module ActiveRecord
197
198
  def becomes(klass)
198
199
  became = klass.new
199
200
  became.instance_variable_set("@attributes", @attributes)
200
- became.instance_variable_set("@changed_attributes", @changed_attributes) if defined?(@changed_attributes)
201
+ changed_attributes = @changed_attributes if defined?(@changed_attributes)
202
+ became.instance_variable_set("@changed_attributes", changed_attributes || {})
201
203
  became.instance_variable_set("@new_record", new_record?)
202
204
  became.instance_variable_set("@destroyed", destroyed?)
203
205
  became.instance_variable_set("@errors", errors)
@@ -367,7 +369,7 @@ module ActiveRecord
367
369
  # # => #<Account id: 1, email: 'account@example.com'>
368
370
  #
369
371
  # Attributes are reloaded from the database, and caches busted, in
370
- # particular the associations cache.
372
+ # particular the associations cache and the QueryCache.
371
373
  #
372
374
  # If the record no longer exists in the database <tt>ActiveRecord::RecordNotFound</tt>
373
375
  # is raised. Otherwise, in addition to the in-place modification the method
@@ -405,6 +407,7 @@ module ActiveRecord
405
407
  def reload(options = nil)
406
408
  clear_aggregation_cache
407
409
  clear_association_cache
410
+ self.class.connection.clear_query_cache
408
411
 
409
412
  fresh_object =
410
413
  if options && options[:lock]
@@ -57,8 +57,10 @@ module ActiveRecord
57
57
  console do |app|
58
58
  require "active_record/railties/console_sandbox" if app.sandbox?
59
59
  require "active_record/base"
60
- console = ActiveSupport::Logger.new(STDERR)
61
- Rails.logger.extend ActiveSupport::Logger.broadcast console
60
+ unless ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDERR, STDOUT)
61
+ console = ActiveSupport::Logger.new(STDERR)
62
+ Rails.logger.extend ActiveSupport::Logger.broadcast console
63
+ end
62
64
  end
63
65
 
64
66
  runner do
@@ -147,8 +149,8 @@ end_warning
147
149
  ActiveSupport.on_load(:active_record) do
148
150
  ActionDispatch::Reloader.send(hook) do
149
151
  if ActiveRecord::Base.connected?
150
- ActiveRecord::Base.clear_reloadable_connections!
151
152
  ActiveRecord::Base.clear_cache!
153
+ ActiveRecord::Base.clear_reloadable_connections!
152
154
  end
153
155
  end
154
156
  end