File tree Expand file tree Collapse file tree 6 files changed +52
-1
lines changed
spec/mongoid/association/referenced Expand file tree Collapse file tree 6 files changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -12,8 +12,28 @@ module Macros
12
12
class_attribute :embedded , instance_reader : false
13
13
class_attribute :embedded_relations
14
14
class_attribute :relations
15
+
16
+ # A hash that maps aliases to their associations. This is used when
17
+ # associations specify the `as` option, or on a referenced association.
18
+ # On a referenced association, this is used to map the foreign key to
19
+ # the association's name. For example, if we had the following
20
+ # relationship:
21
+ #
22
+ # User has_many Accounts
23
+ #
24
+ # User will have an entry in the aliased associations hash:
25
+ #
26
+ # account_ids => accounts
27
+ #
28
+ # Note that on the belongs_to associations, the mapping from
29
+ # foreign key => name is not in the aliased_associations hash, but a
30
+ # mapping from name => foreign key is in the aliased_fields hash.
31
+ #
32
+ # @return [ Hash<String, String> ] The aliased associations hash.
33
+ #
15
34
# @api private
16
35
class_attribute :aliased_associations
36
+
17
37
self . embedded = false
18
38
self . embedded_relations = BSON ::Document . new
19
39
self . relations = BSON ::Document . new
Original file line number Diff line number Diff line change @@ -230,6 +230,7 @@ def synced_save
230
230
end
231
231
232
232
def create_foreign_key_field!
233
+ inverse_class . aliased_associations [ foreign_key ] = name . to_s
233
234
@owner_class . field (
234
235
foreign_key ,
235
236
type : FOREIGN_KEY_FIELD_TYPE ,
Original file line number Diff line number Diff line change @@ -43,7 +43,13 @@ def process_attributes(attrs = nil)
43
43
# @return [ true, false ] True if pending, false if not.
44
44
def pending_attribute? ( key , value )
45
45
name = key . to_s
46
- aliased = aliased_associations [ name ] if aliased_associations . key? ( name )
46
+
47
+ aliased = if aliased_associations . key? ( name )
48
+ aliased_associations [ name ]
49
+ else
50
+ name
51
+ end
52
+
47
53
if relations . has_key? ( aliased )
48
54
pending_relations [ name ] = value
49
55
return true
Original file line number Diff line number Diff line change @@ -3789,4 +3789,11 @@ class Distributor
3789
3789
expect ( signature . contracts . first . signature_ids ) . to eq ( [ signature . id ] )
3790
3790
end
3791
3791
end
3792
+
3793
+ context "when there is a foreign key in the aliased associations" do
3794
+ it "has the correct aliases" do
3795
+ expect ( Dog . aliased_associations [ "breed_ids" ] ) . to eq ( "breeds" )
3796
+ expect ( Breed . aliased_associations [ "dog_ids" ] ) . to eq ( "dogs" )
3797
+ end
3798
+ end
3792
3799
end
Original file line number Diff line number Diff line change 4114
4114
expect ( person2 . posts_count ) . to eq 0
4115
4115
end
4116
4116
end
4117
+
4118
+ context "when there is a foreign key in the aliased associations" do
4119
+ it "has the correct aliases" do
4120
+ expect ( Band . aliased_associations [ "artist_ids" ] ) . to eq ( "artists" )
4121
+ expect ( Artist . aliased_associations . key? ( "band_id" ) ) . to be false
4122
+ expect ( Artist . aliased_fields [ "band" ] ) . to eq ( "band_id" )
4123
+ end
4124
+ end
4117
4125
end
Original file line number Diff line number Diff line change 1139
1139
end
1140
1140
end
1141
1141
end
1142
+
1143
+ context "when there is a foreign key in the aliased associations" do
1144
+ it "has the correct aliases" do
1145
+ # Instances of Driver do not respond to vehicle_id.
1146
+ expect ( Driver . aliased_associations . key? ( "vehicle_id" ) ) . to be false
1147
+ expect ( Vehicle . aliased_associations . key? ( "driver_id" ) ) . to be false
1148
+ expect ( Vehicle . aliased_fields [ "driver" ] ) . to eq ( "driver_id" )
1149
+ end
1150
+ end
1142
1151
end
You can’t perform that action at this time.
0 commit comments