activerecord 4.1.5 → 4.1.6.rc1

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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +161 -0
  3. data/lib/active_record.rb +1 -0
  4. data/lib/active_record/associations.rb +3 -3
  5. data/lib/active_record/associations/alias_tracker.rb +11 -1
  6. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +9 -0
  7. data/lib/active_record/associations/has_many_through_association.rb +5 -1
  8. data/lib/active_record/associations/through_association.rb +10 -6
  9. data/lib/active_record/attribute_methods.rb +8 -5
  10. data/lib/active_record/attribute_methods/primary_key.rb +8 -1
  11. data/lib/active_record/attribute_methods/serialization.rb +6 -1
  12. data/lib/active_record/autosave_association.rb +1 -0
  13. data/lib/active_record/coders/json.rb +13 -0
  14. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +2 -2
  15. data/lib/active_record/connection_adapters/abstract/transaction.rb +1 -0
  16. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +2 -2
  17. data/lib/active_record/connection_adapters/connection_specification.rb +1 -1
  18. data/lib/active_record/connection_adapters/postgresql/oid.rb +8 -2
  19. data/lib/active_record/connection_adapters/postgresql/quoting.rb +1 -1
  20. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +1 -1
  21. data/lib/active_record/counter_cache.rb +1 -1
  22. data/lib/active_record/enum.rb +2 -2
  23. data/lib/active_record/gem_version.rb +2 -2
  24. data/lib/active_record/migration.rb +9 -10
  25. data/lib/active_record/persistence.rb +1 -0
  26. data/lib/active_record/railties/databases.rake +5 -4
  27. data/lib/active_record/reflection.rb +6 -6
  28. data/lib/active_record/relation.rb +8 -1
  29. data/lib/active_record/relation/calculations.rb +5 -2
  30. data/lib/active_record/relation/finder_methods.rb +11 -2
  31. data/lib/active_record/relation/query_methods.rb +15 -3
  32. data/lib/active_record/schema_dumper.rb +2 -1
  33. data/lib/active_record/schema_migration.rb +7 -0
  34. metadata +9 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c2fb22442debba985a6376c3c786841597a57c4
4
- data.tar.gz: 538fd25c6b66cb8c30f8a730afb9737b4718d1ce
3
+ metadata.gz: 02fdf8941d42f0f2f3283a00dd3616750277b096
4
+ data.tar.gz: 11006bed30d01efe124ace3e324bbfa3c819f069
5
5
  SHA512:
6
- metadata.gz: 1279a4c8bf786d49638d67b565995258eedb09161090380fe10eacb620810cf84eeb477129c15b9ba673c4784dbbffe28533530fecfddde5be404003b987b14d
7
- data.tar.gz: f79932b94e2dad4111460f7db455cc78e05ae8f886b8bca26a4ad5d369d13a8917b4167a2091611519ae55699afff32e9de5fb775a472b379b5661a1c96d59f5
6
+ metadata.gz: de48b07e300b9b72eb5cb34cb2a6a12b3bb74764f9dd7fa4cd4ef89c51059da2781d3bba28202c5eb1d1418abde8a33a9ebe337cc6fc168d965a5439ae3b176c
7
+ data.tar.gz: 596770b48517454ce12958f5d9e8f3af669f0473c935c6c04ffa5cbe6dbc526496733fb56f6ae65671258b973254dea9e7cb6943606f4a8829ffbfcaceaf1357
@@ -1,3 +1,164 @@
1
+ ## Rails 4.1.6 (August 19, 2014) ##
2
+
3
+ * Fixed an issue where custom accessor methods (such as those generated by
4
+ `enum`) with the same name as a global method are incorrectly overridden
5
+ when subclassing.
6
+
7
+ Fixes #16288.
8
+
9
+ * Baseclass becomes! subclass.
10
+
11
+ Before this change, a record which changed its STI type, could not be found when updated.
12
+ Setting update_record to the base class, ensures the record can be found.
13
+
14
+ Fixes #14785.
15
+
16
+ *Matthew Draper*, *Earl St Sauver*, *Edo Balvers*
17
+
18
+ * Fix regression on after_commit that didnt fire when having nested transactions.
19
+
20
+ Fixes #16425.
21
+
22
+ *arthurnn*
23
+
24
+ * Define `id_was` to get the previous value of the primary key.
25
+
26
+ Currently when we call id_was and we have a custom primary key name
27
+ Active Record will return the current value of the primary key. This
28
+ make impossible to correctly do an update operation if you change the
29
+ id.
30
+
31
+ Fixes #16413.
32
+
33
+ *Rafael Mendonça França*
34
+
35
+ * Fix the schema dump generated for tables without constraints and with
36
+ primary key with default value of custom PostgreSQL function result.
37
+
38
+ Fixes #16111.
39
+
40
+ *Andrey Novikov*
41
+
42
+ * Restore 4.0 behavior for using serialize attributes with `JSON` as coder.
43
+
44
+ With 4.1.x, `serialize` started returning a string when `JSON` was passed as
45
+ the second attribute. It will now return a hash as per previous versions.
46
+
47
+ Example:
48
+
49
+ class Post < ActiveRecord::Base
50
+ serialize :comment, JSON
51
+ end
52
+
53
+ class Comment
54
+ include ActiveModel::Model
55
+ attr_accessor :category, :text
56
+ end
57
+
58
+ post = Post.create!
59
+ post.comment = Comment.new(category: "Animals", text: "This is a comment about squirrels.")
60
+ post.save!
61
+
62
+ # 4.0
63
+ post.comment # => {"category"=>"Animals", "text"=>"This is a comment about squirrels."}
64
+
65
+ # 4.1 before
66
+ post.comment # => "#<Comment:0x007f80ab48ff98>"
67
+
68
+ # 4.1 after
69
+ post.comment # => {"category"=>"Animals", "text"=>"This is a comment about squirrels."}
70
+
71
+ When using `JSON` as the coder in `serialize`, Active Record will use the
72
+ new `ActiveRecord::Coders::JSON` coder which delegates its `dump/load` to
73
+ `ActiveSupport::JSON.encode/decode`. This ensures special objects are dumped
74
+ correctly using the `#as_json` hook.
75
+
76
+ To keep the previous behaviour, supply a custom coder instead
77
+ ([example](https://gist.github.com/jenncoop/8c4142bbe59da77daa63)).
78
+
79
+ Fixes #15594.
80
+
81
+ *Jenn Cooper*
82
+
83
+ * Fixed error in `reset_counters` when associations have `select` scope.
84
+ (Call to `count` generates invalid SQL.)
85
+
86
+ *Cade Truitt*
87
+
88
+ * After a successful `reload`, `new_record?` is always false.
89
+
90
+ Fixes #12101.
91
+
92
+ *Matthew Draper*
93
+
94
+ * `has_many :through` associations will no longer save the through record
95
+ twice when added in an `after_create` callback defined before the
96
+ associations.
97
+
98
+ *Sean Griffin*
99
+
100
+ * Correctly extract IPv6 addresses from `DATABASE_URI`: the square brackets
101
+ are part of the URI structure, not the actual host.
102
+
103
+ Fixes #15705.
104
+
105
+ *Andy Bakun*, *Aaron Stone*
106
+
107
+ * Don't error when quoting user defined types in PostgreSQL.
108
+
109
+ Fixes #15697.
110
+
111
+ *Sean Griffin*
112
+
113
+ * Ensure both parent IDs are set on join records when both sides of a
114
+ through association are new.
115
+
116
+ *Sean Griffin*
117
+
118
+ * Pluck now works when selecting columns from different tables with the same
119
+ name.
120
+
121
+ Fixes #15649.
122
+
123
+ *Sean Griffin*
124
+
125
+ * `ActiveRecord::FinderMethods.find` with block can handle proc parameter as
126
+ `Enumerable#find` does.
127
+
128
+ Fixes #15382.
129
+
130
+ *James Yang*
131
+
132
+ * `ActiveRecord::SchemaMigration` has no primary key regardless of the
133
+ `primary_key_prefix_type` configuration.
134
+
135
+ Fixes #15051.
136
+
137
+ *JoseLuis Torres*, *Yves Senn*
138
+
139
+ * `rake db:migrate:status` works with legacy migration numbers like `00018_xyz.rb`.
140
+
141
+ Fixes #15538.
142
+
143
+ *Yves Senn*
144
+
145
+ * Fixed `columns_for_distinct` of postgresql adapter to work correctly
146
+ with orders without sort direction modifiers.
147
+
148
+ *Nikolay Kondratyev*
149
+
150
+ * Keep PostgreSQL `hstore` and `json` attributes as `Hash` in `@attributes`.
151
+ Fixes duplication in combination with `store_accessor`.
152
+
153
+ Fixes #15369.
154
+
155
+ *Yves Senn*
156
+
157
+ * `rake railties:install:migrations` respects the order of railties.
158
+
159
+ *Arun Agrawal*
160
+
161
+
1
162
  ## Rails 4.1.5 (August 18, 2014) ##
2
163
 
3
164
  * No changes.
@@ -95,6 +95,7 @@ module ActiveRecord
95
95
 
96
96
  module Coders
97
97
  autoload :YAMLColumn, 'active_record/coders/yaml_column'
98
+ autoload :JSON, 'active_record/coders/json'
98
99
  end
99
100
 
100
101
  module AttributeMethods
@@ -1422,7 +1422,7 @@ module ActiveRecord
1422
1422
  # belongs_to :firm, foreign_key: "client_of"
1423
1423
  # belongs_to :person, primary_key: "name", foreign_key: "person_name"
1424
1424
  # belongs_to :author, class_name: "Person", foreign_key: "author_id"
1425
- # belongs_to :valid_coupon, ->(o) { where "discounts > #{o.payments_count}" },
1425
+ # belongs_to :valid_coupon, ->(o) { where "discounts > ?", o.payments_count },
1426
1426
  # class_name: "Coupon", foreign_key: "coupon_id"
1427
1427
  # belongs_to :attachable, polymorphic: true
1428
1428
  # belongs_to :project, readonly: true
@@ -1588,7 +1588,7 @@ module ActiveRecord
1588
1588
 
1589
1589
  Builder::HasMany.define_callbacks self, middle_reflection
1590
1590
  Reflection.add_reflection self, middle_reflection.name, middle_reflection
1591
- middle_reflection.parent_reflection = [name, habtm_reflection]
1591
+ middle_reflection.parent_reflection = [name.to_sym, habtm_reflection]
1592
1592
 
1593
1593
  include Module.new {
1594
1594
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -1609,7 +1609,7 @@ module ActiveRecord
1609
1609
  end
1610
1610
 
1611
1611
  has_many name, scope, hm_options, &extension
1612
- self._reflections[name].parent_reflection = [name, habtm_reflection]
1612
+ self._reflections[name.to_sym].parent_reflection = [name.to_sym, habtm_reflection]
1613
1613
  end
1614
1614
  end
1615
1615
  end
@@ -32,8 +32,18 @@ module ActiveRecord
32
32
  join.left.downcase.scan(
33
33
  /join(?:\s+\w+)?\s+(\S+\s+)?#{quoted_name}\son/
34
34
  ).size
35
- else
35
+ elsif join.respond_to? :left
36
36
  join.left.table_name == name ? 1 : 0
37
+ else
38
+ # this branch is reached by two tests:
39
+ #
40
+ # activerecord/test/cases/associations/cascaded_eager_loading_test.rb:37
41
+ # with :posts
42
+ #
43
+ # activerecord/test/cases/associations/eager_test.rb:1133
44
+ # with :comments
45
+ #
46
+ 0
37
47
  end
38
48
  end
39
49
 
@@ -72,6 +72,15 @@ module ActiveRecord::Associations::Builder
72
72
  self.right_reflection = _reflect_on_association(rhs_name)
73
73
  end
74
74
 
75
+ def hash
76
+ object_id.hash
77
+ end
78
+
79
+ def ==(other)
80
+ equal?(other)
81
+ end
82
+ alias :eql? :==
83
+
75
84
  }
76
85
 
77
86
  join_model.name = "HABTM_#{association_name.to_s.camelize}"
@@ -187,7 +187,11 @@ module ActiveRecord
187
187
  def through_records_for(record)
188
188
  attributes = construct_join_attributes(record)
189
189
  candidates = Array.wrap(through_association.target)
190
- candidates.find_all { |c| c.attributes.slice(*attributes.keys) == attributes }
190
+ candidates.find_all do |c|
191
+ attributes.all? do |key, value|
192
+ c.public_send(key) == value
193
+ end
194
+ end
191
195
  end
192
196
 
193
197
  def delete_through_records(records)
@@ -41,12 +41,16 @@ module ActiveRecord
41
41
  def construct_join_attributes(*records)
42
42
  ensure_mutable
43
43
 
44
- join_attributes = {
45
- source_reflection.foreign_key =>
46
- records.map { |record|
47
- record.send(source_reflection.association_primary_key(reflection.klass))
48
- }
49
- }
44
+ if source_reflection.association_primary_key(reflection.klass) == reflection.klass.primary_key
45
+ join_attributes = { source_reflection.name => records }
46
+ else
47
+ join_attributes = {
48
+ source_reflection.foreign_key =>
49
+ records.map { |record|
50
+ record.send(source_reflection.association_primary_key(reflection.klass))
51
+ }
52
+ }
53
+ end
50
54
 
51
55
  if options[:source_type]
52
56
  join_attributes[source_reflection.foreign_type] =
@@ -51,6 +51,8 @@ module ActiveRecord
51
51
  def method_body; raise NotImplementedError; end
52
52
  end
53
53
 
54
+ class GeneratedAttributeMethods < Module; end # :nodoc:
55
+
54
56
  module ClassMethods
55
57
  def inherited(child_class) #:nodoc:
56
58
  child_class.initialize_generated_modules
@@ -58,7 +60,7 @@ module ActiveRecord
58
60
  end
59
61
 
60
62
  def initialize_generated_modules # :nodoc:
61
- @generated_attribute_methods = Module.new { extend Mutex_m }
63
+ @generated_attribute_methods = GeneratedAttributeMethods.new { extend Mutex_m }
62
64
  @attribute_methods_generated = false
63
65
  include @generated_attribute_methods
64
66
  end
@@ -107,10 +109,11 @@ module ActiveRecord
107
109
  if superclass == Base
108
110
  super
109
111
  else
110
- # If B < A and A defines its own attribute method, then we don't want to overwrite that.
111
- defined = method_defined_within?(method_name, superclass, superclass.generated_attribute_methods)
112
- base_defined = Base.method_defined?(method_name) || Base.private_method_defined?(method_name)
113
- defined && !base_defined || super
112
+ # If ThisClass < ... < SomeSuperClass < ... < Base and SomeSuperClass
113
+ # defines its own attribute method, then we don't want to overwrite that.
114
+ defined = method_defined_within?(method_name, superclass, Base) &&
115
+ ! superclass.instance_method(method_name).owner.is_a?(GeneratedAttributeMethods)
116
+ defined || super
114
117
  end
115
118
  end
116
119
 
@@ -15,6 +15,7 @@ module ActiveRecord
15
15
 
16
16
  # Returns the primary key value.
17
17
  def id
18
+ return unless self.class.primary_key
18
19
  sync_with_transaction_state
19
20
  read_attribute(self.class.primary_key)
20
21
  end
@@ -37,6 +38,12 @@ module ActiveRecord
37
38
  read_attribute_before_type_cast(self.class.primary_key)
38
39
  end
39
40
 
41
+ # Returns the primary key previous value.
42
+ def id_was
43
+ sync_with_transaction_state
44
+ attribute_was(self.class.primary_key)
45
+ end
46
+
40
47
  protected
41
48
 
42
49
  def attribute_method?(attr_name)
@@ -52,7 +59,7 @@ module ActiveRecord
52
59
  end
53
60
  end
54
61
 
55
- ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast).to_set
62
+ ID_ATTRIBUTE_METHODS = %w(id id= id? id_before_type_cast id_was).to_set
56
63
 
57
64
  def dangerous_attribute_method?(method_name)
58
65
  super && !ID_ATTRIBUTE_METHODS.include?(method_name)
@@ -52,7 +52,12 @@ module ActiveRecord
52
52
  def serialize(attr_name, class_name_or_coder = Object)
53
53
  include Behavior
54
54
 
55
- coder = if [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
55
+ # When ::JSON is used, force it to go through the Active Support JSON encoder
56
+ # to ensure special objects (e.g. Active Record models) are dumped correctly
57
+ # using the #as_json hook.
58
+ coder = if class_name_or_coder == ::JSON
59
+ Coders::JSON
60
+ elsif [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) }
56
61
  class_name_or_coder
57
62
  else
58
63
  Coders::YAMLColumn.new(class_name_or_coder)
@@ -363,6 +363,7 @@ module ActiveRecord
363
363
 
364
364
  raise ActiveRecord::Rollback unless saved
365
365
  end
366
+ @new_record_before_save = false
366
367
  end
367
368
 
368
369
  # reconstruct the scope now that we know the owner's id
@@ -0,0 +1,13 @@
1
+ module ActiveRecord
2
+ module Coders # :nodoc:
3
+ class JSON # :nodoc:
4
+ def self.dump(obj)
5
+ ActiveSupport::JSON.encode(obj)
6
+ end
7
+
8
+ def self.load(json)
9
+ ActiveSupport::JSON.decode(json) unless json.nil?
10
+ end
11
+ end
12
+ end
13
+ end
@@ -236,8 +236,8 @@ module ActiveRecord
236
236
 
237
237
  @spec = spec
238
238
 
239
- @checkout_timeout = spec.config[:checkout_timeout] || 5
240
- @dead_connection_timeout = spec.config[:dead_connection_timeout] || 5
239
+ @checkout_timeout = (spec.config[:checkout_timeout] && spec.config[:checkout_timeout].to_f) || 5
240
+ @dead_connection_timeout = (spec.config[:dead_connection_timeout] && spec.config[:dead_connection_timeout].to_f) || 5
241
241
  @reaper = Reaper.new self, spec.config[:reaping_frequency]
242
242
  @reaper.run
243
243
 
@@ -201,6 +201,7 @@ module ActiveRecord
201
201
  @state.set_state(:committed)
202
202
  @state.parent = parent.state
203
203
  connection.release_savepoint
204
+ records.each { |r| parent.add_record(r) }
204
205
  end
205
206
  end
206
207
  end
@@ -753,8 +753,8 @@ module ActiveRecord
753
753
  # Make MySQL reject illegal values rather than truncating or blanking them, see
754
754
  # http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_all_tables
755
755
  # If the user has provided another value for sql_mode, don't replace it.
756
- if strict_mode? && !variables.has_key?('sql_mode')
757
- variables['sql_mode'] = 'STRICT_ALL_TABLES'
756
+ unless variables.has_key?('sql_mode')
757
+ variables['sql_mode'] = strict_mode? ? 'STRICT_ALL_TABLES' : ''
758
758
  end
759
759
 
760
760
  # NAMES does not have an equals sign, see
@@ -86,7 +86,7 @@ module ActiveRecord
86
86
  "password" => uri.password,
87
87
  "port" => uri.port,
88
88
  "database" => database_from_path,
89
- "host" => uri.host })
89
+ "host" => uri.hostname })
90
90
  end
91
91
  end
92
92
 
@@ -231,7 +231,10 @@ module ActiveRecord
231
231
 
232
232
  class Hstore < Type
233
233
  def type_cast_for_write(value)
234
- ConnectionAdapters::PostgreSQLColumn.hstore_to_string value
234
+ # roundtrip to ensure uniform uniform types
235
+ # TODO: This is not an efficient solution.
236
+ stringified = ConnectionAdapters::PostgreSQLColumn.hstore_to_string(value)
237
+ type_cast(stringified)
235
238
  end
236
239
 
237
240
  def type_cast(value)
@@ -255,7 +258,10 @@ module ActiveRecord
255
258
 
256
259
  class Json < Type
257
260
  def type_cast_for_write(value)
258
- ConnectionAdapters::PostgreSQLColumn.json_to_string value
261
+ # roundtrip to ensure uniform uniform types
262
+ # TODO: This is not an efficient solution.
263
+ stringified = ConnectionAdapters::PostgreSQLColumn.json_to_string(value)
264
+ type_cast(stringified)
259
265
  end
260
266
 
261
267
  def type_cast(value)
@@ -16,7 +16,7 @@ module ActiveRecord
16
16
 
17
17
  # Quotes PostgreSQL-specific data types for SQL input.
18
18
  def quote(value, column = nil) #:nodoc:
19
- return super unless column
19
+ return super unless column && column.type
20
20
 
21
21
  sql_type = type_to_sql(column.type, column.limit, column.precision, column.scale)
22
22
 
@@ -493,7 +493,7 @@ module ActiveRecord
493
493
  # Convert Arel node to string
494
494
  s = s.to_sql unless s.is_a?(String)
495
495
  # Remove any ASC/DESC modifiers
496
- s.gsub(/\s+(ASC|DESC)\s*(NULLS\s+(FIRST|LAST)\s*)?/i, '')
496
+ s.gsub(/\s+(?:ASC|DESC)?\s*(?:NULLS\s+(?:FIRST|LAST)\s*)?/i, '')
497
497
  }.reject(&:blank?).map.with_index { |column, i| "#{column} AS alias_#{i}" }
498
498
 
499
499
  [super, *order_columns].join(', ')
@@ -33,7 +33,7 @@ module ActiveRecord
33
33
  counter_name = reflection.counter_cache_column
34
34
 
35
35
  stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({
36
- arel_table[counter_name] => object.send(association).count
36
+ arel_table[counter_name] => object.send(association).count(:all)
37
37
  }, primary_key)
38
38
  connection.update stmt
39
39
  end
@@ -67,12 +67,12 @@ module ActiveRecord
67
67
  #
68
68
  # Where conditions on an enum attribute must use the ordinal value of an enum.
69
69
  module Enum
70
- def self.extended(base)
70
+ def self.extended(base) # :nodoc:
71
71
  base.class_attribute(:defined_enums)
72
72
  base.defined_enums = {}
73
73
  end
74
74
 
75
- def inherited(base)
75
+ def inherited(base) # :nodoc:
76
76
  base.defined_enums = defined_enums.deep_dup
77
77
  super
78
78
  end
@@ -7,8 +7,8 @@ module ActiveRecord
7
7
  module VERSION
8
8
  MAJOR = 4
9
9
  MINOR = 1
10
- TINY = 5
11
- PRE = nil
10
+ TINY = 6
11
+ PRE = "rc1"
12
12
 
13
13
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
14
14
  end
@@ -711,7 +711,7 @@ module ActiveRecord
711
711
  if ActiveRecord::Base.timestamped_migrations
712
712
  [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max
713
713
  else
714
- "%.3d" % number
714
+ SchemaMigration.normalize_migration_number(number)
715
715
  end
716
716
  end
717
717
 
@@ -826,21 +826,20 @@ module ActiveRecord
826
826
  SchemaMigration.table_name
827
827
  end
828
828
 
829
- def get_all_versions
830
- SchemaMigration.all.map { |x| x.version.to_i }.sort
829
+ def get_all_versions(connection = Base.connection)
830
+ if connection.table_exists?(schema_migrations_table_name)
831
+ SchemaMigration.all.map { |x| x.version.to_i }.sort
832
+ else
833
+ []
834
+ end
831
835
  end
832
836
 
833
837
  def current_version(connection = Base.connection)
834
- sm_table = schema_migrations_table_name
835
- if connection.table_exists?(sm_table)
836
- get_all_versions.max || 0
837
- else
838
- 0
839
- end
838
+ get_all_versions(connection).max || 0
840
839
  end
841
840
 
842
841
  def needs_migration?(connection = Base.connection)
843
- current_version(connection) < last_version
842
+ (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0
844
843
  end
845
844
 
846
845
  def last_version
@@ -399,6 +399,7 @@ module ActiveRecord
399
399
  @column_types = self.class.column_types
400
400
  @column_types_override = fresh_object.instance_variable_get('@column_types_override')
401
401
  @attributes_cache = {}
402
+ @new_record = false
402
403
  self
403
404
  end
404
405
 
@@ -87,14 +87,15 @@ db_namespace = namespace :db do
87
87
  next # means "return" for rake task
88
88
  end
89
89
  db_list = ActiveRecord::Base.connection.select_values("SELECT version FROM #{ActiveRecord::Migrator.schema_migrations_table_name}")
90
- db_list.map! { |version| "%.3d" % version }
90
+ db_list.map! { |version| ActiveRecord::SchemaMigration.normalize_migration_number(version) }
91
91
  file_list = []
92
92
  ActiveRecord::Migrator.migrations_paths.each do |path|
93
93
  Dir.foreach(path) do |file|
94
94
  # match "20091231235959_some_name.rb" and "001_some_name.rb" pattern
95
95
  if match_data = /^(\d{3,})_(.+)\.rb$/.match(file)
96
- status = db_list.delete(match_data[1]) ? 'up' : 'down'
97
- file_list << [status, match_data[1], match_data[2].humanize]
96
+ version = ActiveRecord::SchemaMigration.normalize_migration_number(match_data[1])
97
+ status = db_list.delete(version) ? 'up' : 'down'
98
+ file_list << [status, version, match_data[2].humanize]
98
99
  end
99
100
  end
100
101
  end
@@ -367,7 +368,7 @@ namespace :railties do
367
368
  task :migrations => :'db:load_config' do
368
369
  to_load = ENV['FROM'].blank? ? :all : ENV['FROM'].split(",").map {|n| n.strip }
369
370
  railties = {}
370
- Rails.application.railties.each do |railtie|
371
+ Rails.application.migration_railties.each do |railtie|
371
372
  next unless to_load == :all || to_load.include?(railtie.railtie_name)
372
373
 
373
374
  if railtie.respond_to?(:paths) && (path = railtie.paths['db/migrate'].first)
@@ -22,11 +22,11 @@ module ActiveRecord
22
22
  end
23
23
 
24
24
  def self.add_reflection(ar, name, reflection)
25
- ar._reflections = ar._reflections.merge(name => reflection)
25
+ ar._reflections = ar._reflections.merge(name.to_sym => reflection)
26
26
  end
27
27
 
28
28
  def self.add_aggregate_reflection(ar, name, reflection)
29
- ar.aggregate_reflections = ar.aggregate_reflections.merge(name => reflection)
29
+ ar.aggregate_reflections = ar.aggregate_reflections.merge(name.to_sym => reflection)
30
30
  end
31
31
 
32
32
  # \Reflection enables to interrogate Active Record classes and objects
@@ -48,7 +48,7 @@ module ActiveRecord
48
48
  # Account.reflect_on_aggregation(:balance) # => the balance AggregateReflection
49
49
  #
50
50
  def reflect_on_aggregation(aggregation)
51
- aggregate_reflections[aggregation]
51
+ aggregate_reflections[aggregation.to_sym]
52
52
  end
53
53
 
54
54
  # Returns a Hash of name of the reflection as the key and a AssociationReflection as the value.
@@ -92,12 +92,12 @@ module ActiveRecord
92
92
  #
93
93
  # @api public
94
94
  def reflect_on_association(association)
95
- reflections[association]
95
+ reflections[association.to_sym]
96
96
  end
97
97
 
98
98
  # @api private
99
99
  def _reflect_on_association(association) #:nodoc:
100
- _reflections[association]
100
+ _reflections[association.to_sym]
101
101
  end
102
102
 
103
103
  # Returns an array of AssociationReflection objects for all associations which have <tt>:autosave</tt> enabled.
@@ -432,7 +432,7 @@ module ActiveRecord
432
432
  # returns either nil or the inverse association name that it finds.
433
433
  def automatic_inverse_of
434
434
  if can_find_inverse_of_automatically?(self)
435
- inverse_name = ActiveSupport::Inflector.underscore(active_record.name).to_sym
435
+ inverse_name = ActiveSupport::Inflector.underscore(options[:as] || active_record.name).to_sym
436
436
 
437
437
  begin
438
438
  reflection = klass._reflect_on_association(inverse_name)
@@ -72,7 +72,14 @@ module ActiveRecord
72
72
 
73
73
  def _update_record(values, id, id_was) # :nodoc:
74
74
  substitutes, binds = substitute_values values
75
- um = @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes, @klass.primary_key)
75
+
76
+ scope = @klass.unscoped
77
+
78
+ if @klass.finder_needs_type_condition?
79
+ scope.unscope!(where: @klass.inheritance_column)
80
+ end
81
+
82
+ um = scope.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes, @klass.primary_key)
76
83
 
77
84
  @klass.connection.update(
78
85
  um,
@@ -176,8 +176,11 @@ module ActiveRecord
176
176
  }
177
177
  end
178
178
 
179
- result = result.map do |attributes|
180
- values = klass.initialize_attributes(attributes).values
179
+ result = result.rows.map do |values|
180
+ values = result.columns.zip(values).map do |column_name, value|
181
+ single_attr_hash = { column_name => value }
182
+ klass.initialize_attributes(single_attr_hash).values.first
183
+ end
181
184
 
182
185
  columns.zip(values).map { |column, value| column.type_cast value }
183
186
  end
@@ -63,7 +63,7 @@ module ActiveRecord
63
63
  # # returns an Array of the required fields, available since Rails 3.1.
64
64
  def find(*args)
65
65
  if block_given?
66
- to_a.find { |*block_args| yield(*block_args) }
66
+ to_a.find(*args) { |*block_args| yield(*block_args) }
67
67
  else
68
68
  find_with_ids(*args)
69
69
  end
@@ -323,7 +323,16 @@ module ActiveRecord
323
323
  private
324
324
 
325
325
  def find_with_associations
326
- join_dependency = construct_join_dependency
326
+ # NOTE: the JoinDependency constructed here needs to know about
327
+ # any joins already present in `self`, so pass them in
328
+ #
329
+ # failing to do so means that in cases like activerecord/test/cases/associations/inner_join_association_test.rb:136
330
+ # incorrect SQL is generated. In that case, the join dependency for
331
+ # SpecialCategorizations is constructed without knowledge of the
332
+ # preexisting join in joins_values to categorizations (by way of
333
+ # the `has_many :through` for categories).
334
+ #
335
+ join_dependency = construct_join_dependency(joins_values)
327
336
 
328
337
  aliases = join_dependency.aliases
329
338
  relation = select aliases.columns
@@ -943,9 +943,7 @@ module ActiveRecord
943
943
  opts = PredicateBuilder.resolve_column_aliases(klass, opts)
944
944
  attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
945
945
 
946
- attributes.values.grep(ActiveRecord::Relation) do |rel|
947
- self.bind_values += rel.bind_values
948
- end
946
+ add_relations_to_bind_values(attributes)
949
947
 
950
948
  PredicateBuilder.build_from_hash(klass, attributes, table)
951
949
  else
@@ -1098,5 +1096,19 @@ module ActiveRecord
1098
1096
  raise ArgumentError, "The method .#{method_name}() must contain arguments."
1099
1097
  end
1100
1098
  end
1099
+
1100
+ # This function is recursive just for better readablity.
1101
+ # #where argument doesn't support more than one level nested hash in real world.
1102
+ def add_relations_to_bind_values(attributes)
1103
+ if attributes.is_a?(Hash)
1104
+ attributes.each_value do |value|
1105
+ if value.is_a?(ActiveRecord::Relation)
1106
+ self.bind_values += value.bind_values
1107
+ else
1108
+ add_relations_to_bind_values(value)
1109
+ end
1110
+ end
1111
+ end
1112
+ end
1101
1113
  end
1102
1114
  end
@@ -112,7 +112,8 @@ HEADER
112
112
  # first dump primary key column
113
113
  if @connection.respond_to?(:pk_and_sequence_for)
114
114
  pk, _ = @connection.pk_and_sequence_for(table)
115
- elsif @connection.respond_to?(:primary_key)
115
+ end
116
+ if !pk && @connection.respond_to?(:primary_key)
116
117
  pk = @connection.primary_key(table)
117
118
  end
118
119
 
@@ -5,6 +5,9 @@ require 'active_record/base'
5
5
  module ActiveRecord
6
6
  class SchemaMigration < ActiveRecord::Base
7
7
  class << self
8
+ def primary_key
9
+ nil
10
+ end
8
11
 
9
12
  def table_name
10
13
  "#{table_name_prefix}#{ActiveRecord::Base.schema_migrations_table_name}#{table_name_suffix}"
@@ -36,6 +39,10 @@ module ActiveRecord
36
39
  connection.drop_table(table_name)
37
40
  end
38
41
  end
42
+
43
+ def normalize_migration_number(number)
44
+ "%.3d" % number.to_i
45
+ end
39
46
  end
40
47
 
41
48
  def version
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.5
4
+ version: 4.1.6.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-18 00:00:00.000000000 Z
11
+ date: 2014-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.5
19
+ version: 4.1.6.rc1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 4.1.5
26
+ version: 4.1.6.rc1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '='
32
32
  - !ruby/object:Gem::Version
33
- version: 4.1.5
33
+ version: 4.1.6.rc1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
- version: 4.1.5
40
+ version: 4.1.6.rc1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: arel
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -117,6 +117,7 @@ files:
117
117
  - lib/active_record/autosave_association.rb
118
118
  - lib/active_record/base.rb
119
119
  - lib/active_record/callbacks.rb
120
+ - lib/active_record/coders/json.rb
120
121
  - lib/active_record/coders/yaml_column.rb
121
122
  - lib/active_record/connection_adapters/abstract/connection_pool.rb
122
123
  - lib/active_record/connection_adapters/abstract/database_limits.rb
@@ -242,9 +243,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
242
243
  version: 1.9.3
243
244
  required_rubygems_version: !ruby/object:Gem::Requirement
244
245
  requirements:
245
- - - ">="
246
+ - - ">"
246
247
  - !ruby/object:Gem::Version
247
- version: '0'
248
+ version: 1.3.1
248
249
  requirements: []
249
250
  rubyforge_project:
250
251
  rubygems_version: 2.3.0