activerecord 3.1.1.rc1 → 3.1.1.rc2
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.
- data/CHANGELOG +22 -0
- data/lib/active_record/associations.rb +13 -7
- data/lib/active_record/associations/association_scope.rb +6 -5
- data/lib/active_record/associations/belongs_to_association.rb +5 -1
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +0 -4
- data/lib/active_record/associations/collection_proxy.rb +1 -1
- data/lib/active_record/attribute_methods/primary_key.rb +0 -1
- data/lib/active_record/base.rb +4 -0
- data/lib/active_record/reflection.rb +11 -15
- data/lib/active_record/relation/query_methods.rb +29 -0
- data/lib/active_record/version.rb +1 -1
- metadata +10 -10
data/CHANGELOG
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
*Rails 3.1.1 (unreleased)*
|
2
2
|
|
3
|
+
* Add deprecation for the preload_associations method. Fixes #3022.
|
4
|
+
|
5
|
+
[Jon Leighton]
|
6
|
+
|
7
|
+
* Don't require a DB connection when loading a model that uses set_primary_key. GH #2807.
|
8
|
+
|
9
|
+
[Jon Leighton]
|
10
|
+
|
11
|
+
* Fix using select() with a habtm association, e.g. Person.friends.select(:name). GH #3030 and
|
12
|
+
#2923.
|
13
|
+
|
14
|
+
[Hendy Tanata]
|
15
|
+
|
16
|
+
* Fix belongs_to polymorphic with custom primary key on target. GH #3104.
|
17
|
+
|
18
|
+
[Jon Leighton]
|
19
|
+
|
20
|
+
* CollectionProxy#replace should change the DB records rather than just mutating the array.
|
21
|
+
Fixes #3020.
|
22
|
+
|
23
|
+
[Jon Leighton]
|
24
|
+
|
3
25
|
* LRU cache in mysql and sqlite are now per-process caches.
|
4
26
|
|
5
27
|
* lib/active_record/connection_adapters/mysql_adapter.rb: LRU cache
|
@@ -5,6 +5,7 @@ require 'active_support/core_ext/object/blank'
|
|
5
5
|
require 'active_support/core_ext/string/conversions'
|
6
6
|
require 'active_support/core_ext/module/remove_method'
|
7
7
|
require 'active_support/core_ext/class/attribute'
|
8
|
+
require 'active_support/deprecation'
|
8
9
|
|
9
10
|
module ActiveRecord
|
10
11
|
class InverseOfAssociationNotFoundError < ActiveRecordError #:nodoc:
|
@@ -76,12 +77,6 @@ module ActiveRecord
|
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
79
|
-
class HasAndBelongsToManyAssociationWithPrimaryKeyError < ActiveRecordError #:nodoc:
|
80
|
-
def initialize(reflection)
|
81
|
-
super("Primary key is not allowed in a has_and_belongs_to_many join table (#{reflection.options[:join_table]}).")
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
80
|
class HasAndBelongsToManyAssociationForeignKeyNeeded < ActiveRecordError #:nodoc:
|
86
81
|
def initialize(reflection)
|
87
82
|
super("Cannot create self referential has_and_belongs_to_many association on '#{reflection.class_name rescue nil}##{reflection.name rescue nil}'. :association_foreign_key cannot be the same as the :foreign_key.")
|
@@ -1576,10 +1571,21 @@ module ActiveRecord
|
|
1576
1571
|
# has_and_belongs_to_many :categories, :join_table => "prods_cats"
|
1577
1572
|
# has_and_belongs_to_many :categories, :readonly => true
|
1578
1573
|
# has_and_belongs_to_many :active_projects, :join_table => 'developers_projects', :delete_sql =>
|
1579
|
-
#
|
1574
|
+
# "DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}"
|
1580
1575
|
def has_and_belongs_to_many(name, options = {}, &extension)
|
1581
1576
|
Builder::HasAndBelongsToMany.build(self, name, options, &extension)
|
1582
1577
|
end
|
1578
|
+
|
1579
|
+
protected
|
1580
|
+
|
1581
|
+
def preload_associations(records, associations, options = {}) #:nodoc:
|
1582
|
+
ActiveSupport::Deprecation.warn(
|
1583
|
+
"preload_associations(records, associations, options = {}) is deprecated. Use " \
|
1584
|
+
"ActiveRecord::Associations::Preloader.new(records, associations, options = {}).run " \
|
1585
|
+
"instead."
|
1586
|
+
)
|
1587
|
+
Preloader.new(records, associations, options).run
|
1588
|
+
end
|
1583
1589
|
end
|
1584
1590
|
end
|
1585
1591
|
end
|
@@ -42,10 +42,6 @@ module ActiveRecord
|
|
42
42
|
select_value ||= options[:uniq] && "DISTINCT #{reflection.quoted_table_name}.*"
|
43
43
|
end
|
44
44
|
|
45
|
-
if reflection.macro == :has_and_belongs_to_many
|
46
|
-
select_value ||= reflection.klass.arel_table[Arel.star]
|
47
|
-
end
|
48
|
-
|
49
45
|
select_value
|
50
46
|
end
|
51
47
|
|
@@ -68,7 +64,12 @@ module ActiveRecord
|
|
68
64
|
end
|
69
65
|
|
70
66
|
if reflection.source_macro == :belongs_to
|
71
|
-
|
67
|
+
if reflection.options[:polymorphic]
|
68
|
+
key = reflection.association_primary_key(klass)
|
69
|
+
else
|
70
|
+
key = reflection.association_primary_key
|
71
|
+
end
|
72
|
+
|
72
73
|
foreign_key = reflection.foreign_key
|
73
74
|
else
|
74
75
|
key = reflection.foreign_key
|
@@ -45,7 +45,11 @@ module ActiveRecord
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def replace_keys(record)
|
48
|
-
|
48
|
+
if record
|
49
|
+
owner[reflection.foreign_key] = record[reflection.association_primary_key(record.class)]
|
50
|
+
else
|
51
|
+
owner[reflection.foreign_key] = nil
|
52
|
+
end
|
49
53
|
end
|
50
54
|
|
51
55
|
def foreign_key_present?
|
@@ -37,10 +37,6 @@ module ActiveRecord::Associations::Builder
|
|
37
37
|
model.send(:undecorated_table_name, model.to_s),
|
38
38
|
model.send(:undecorated_table_name, reflection.class_name)
|
39
39
|
)
|
40
|
-
|
41
|
-
if model.connection.supports_primary_key? && (model.connection.primary_key(reflection.options[:join_table]) rescue false)
|
42
|
-
raise ActiveRecord::HasAndBelongsToManyAssociationWithPrimaryKeyError.new(reflection)
|
43
|
-
end
|
44
40
|
end
|
45
41
|
|
46
42
|
# Generates a join table name from two provided table names.
|
@@ -53,7 +53,7 @@ module ActiveRecord
|
|
53
53
|
|
54
54
|
delegate :select, :find, :first, :last,
|
55
55
|
:build, :create, :create!,
|
56
|
-
:concat, :delete_all, :destroy_all, :delete, :destroy, :uniq,
|
56
|
+
:concat, :replace, :delete_all, :destroy_all, :delete, :destroy, :uniq,
|
57
57
|
:sum, :count, :size, :length, :empty?,
|
58
58
|
:any?, :many?, :include?,
|
59
59
|
:to => :@association
|
data/lib/active_record/base.rb
CHANGED
@@ -705,6 +705,10 @@ module ActiveRecord #:nodoc:
|
|
705
705
|
|
706
706
|
# Returns an array of column objects for the table associated with this class.
|
707
707
|
def columns
|
708
|
+
if defined?(@primary_key)
|
709
|
+
connection_pool.primary_keys[table_name] ||= primary_key
|
710
|
+
end
|
711
|
+
|
708
712
|
connection_pool.columns[table_name]
|
709
713
|
end
|
710
714
|
|
@@ -223,11 +223,9 @@ module ActiveRecord
|
|
223
223
|
@association_foreign_key ||= options[:association_foreign_key] || class_name.foreign_key
|
224
224
|
end
|
225
225
|
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
!options[:polymorphic] && klass.primary_key ||
|
230
|
-
'id'
|
226
|
+
# klass option is necessary to support loading polymorphic associations
|
227
|
+
def association_primary_key(klass = nil)
|
228
|
+
options[:primary_key] || (klass || self.klass).primary_key
|
231
229
|
end
|
232
230
|
|
233
231
|
def active_record_primary_key
|
@@ -475,17 +473,15 @@ module ActiveRecord
|
|
475
473
|
# We want to use the klass from this reflection, rather than just delegate straight to
|
476
474
|
# the source_reflection, because the source_reflection may be polymorphic. We still
|
477
475
|
# need to respect the source_reflection's :primary_key option, though.
|
478
|
-
def association_primary_key
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
source_reflection = source_reflection.source_reflection
|
485
|
-
end
|
486
|
-
|
487
|
-
source_reflection.options[:primary_key] || klass.primary_key
|
476
|
+
def association_primary_key(klass = self.klass)
|
477
|
+
# Get the "actual" source reflection if the immediate source reflection has a
|
478
|
+
# source reflection itself
|
479
|
+
source_reflection = self.source_reflection
|
480
|
+
while source_reflection.source_reflection
|
481
|
+
source_reflection = source_reflection.source_reflection
|
488
482
|
end
|
483
|
+
|
484
|
+
source_reflection.options[:primary_key] || klass.primary_key
|
489
485
|
end
|
490
486
|
|
491
487
|
# Gets an array of possible <tt>:through</tt> source reflection names:
|
@@ -37,6 +37,35 @@ module ActiveRecord
|
|
37
37
|
relation
|
38
38
|
end
|
39
39
|
|
40
|
+
# Works in two unique ways.
|
41
|
+
#
|
42
|
+
# First: takes a block so it can be used just like Array#select.
|
43
|
+
#
|
44
|
+
# Model.scoped.select { |m| m.field == value }
|
45
|
+
#
|
46
|
+
# This will build an array of objects from the database for the scope,
|
47
|
+
# converting them into an array and iterating through them using Array#select.
|
48
|
+
#
|
49
|
+
# Second: Modifies the SELECT statement for the query so that only certain
|
50
|
+
# fields are retrieved:
|
51
|
+
#
|
52
|
+
# >> Model.select(:field)
|
53
|
+
# => [#<Model field:value>]
|
54
|
+
#
|
55
|
+
# Although in the above example it looks as though this method returns an
|
56
|
+
# array, it actually returns a relation object and can have other query
|
57
|
+
# methods appended to it, such as the other methods in ActiveRecord::QueryMethods.
|
58
|
+
#
|
59
|
+
# This method will also take multiple parameters:
|
60
|
+
#
|
61
|
+
# >> Model.select(:field, :other_field, :and_one_more)
|
62
|
+
# => [#<Model field: "value", other_field: "value", and_one_more: "value">]
|
63
|
+
#
|
64
|
+
# Any attributes that do not have fields retrieved by a select
|
65
|
+
# will return `nil` when the getter method for that attribute is used:
|
66
|
+
#
|
67
|
+
# >> Model.select(:field).first.other_field
|
68
|
+
# => nil
|
40
69
|
def select(value = Proc.new)
|
41
70
|
if block_given?
|
42
71
|
to_a.select {|*block_args| value.call(*block_args) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 977940595
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 1
|
9
9
|
- 1
|
10
|
-
-
|
11
|
-
version: 3.1.1.
|
10
|
+
- rc2
|
11
|
+
version: 3.1.1.rc2
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- David Heinemeier Hansson
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-09-
|
19
|
+
date: 2011-09-29 00:00:00 -03:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -27,13 +27,13 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - "="
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
hash:
|
30
|
+
hash: 977940595
|
31
31
|
segments:
|
32
32
|
- 3
|
33
33
|
- 1
|
34
34
|
- 1
|
35
|
-
-
|
36
|
-
version: 3.1.1.
|
35
|
+
- rc2
|
36
|
+
version: 3.1.1.rc2
|
37
37
|
type: :runtime
|
38
38
|
version_requirements: *id001
|
39
39
|
- !ruby/object:Gem::Dependency
|
@@ -44,13 +44,13 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
hash:
|
47
|
+
hash: 977940595
|
48
48
|
segments:
|
49
49
|
- 3
|
50
50
|
- 1
|
51
51
|
- 1
|
52
|
-
-
|
53
|
-
version: 3.1.1.
|
52
|
+
- rc2
|
53
|
+
version: 3.1.1.rc2
|
54
54
|
type: :runtime
|
55
55
|
version_requirements: *id002
|
56
56
|
- !ruby/object:Gem::Dependency
|