Skip to content

Commit 3bf0d96

Browse files
authored
MONGOID-4953 Update the updated_at field when updating a HABTM association (#5219)
* MONGOID-4953 dont update updated_at when modifying _ids field * MONGOID-4953 update HABTM foreign key on update * MONGOID-4953 add timestamp tests to document the behavior as described in the PR description * MONGOID-4953 remove comments * MONGOID-4953 fix failures * MONGOID-4953 add a note in documentation * MONGOID-4953 add timestamp tests after reload * MONGOID-4953 remove code that updates updated_at * Update docs and add pending * MONGOID-4953 add pending
1 parent ba1385e commit 3bf0d96

File tree

7 files changed

+517
-17
lines changed

7 files changed

+517
-17
lines changed

docs/reference/associations.txt

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ the ids only in one document using the ``inverse_of: nil`` option:
323323
A one-sided ``has_and_belongs_to_many`` association is, naturally, only
324324
usable from the model where it is defined.
325325

326+
.. note::
327+
328+
Given two models, A and B where A ``has_and_belongs_to_many`` B,
329+
when adding a document of type B to the HABTM association on a document of
330+
type A, Mongoid will not update the ``updated_at`` field for the document of
331+
type A, but will update the ``updated_at`` field for the document of type B.
326332

327333
Querying Referenced Associations
328334
--------------------------------
@@ -1270,26 +1276,26 @@ to non-existent documents via associations:
12701276

12711277
class Band
12721278
include Mongoid::Document
1273-
1279+
12741280
has_many :albums
12751281
end
12761282

12771283
class Album
12781284
include Mongoid::Document
1279-
1285+
12801286
belongs_to :band
12811287
end
1282-
1288+
12831289
band = Band.new
12841290
album = Album.create!(band: band)
1285-
1291+
12861292
# The band is not persisted at this point.
1287-
1293+
12881294
album.reload
1289-
1295+
12901296
album.band_id
12911297
# => BSON::ObjectId('6257699753aefe153121a3d5')
1292-
1298+
12931299
# Band does not exist.
12941300
album.band
12951301
# => nil
@@ -1301,26 +1307,26 @@ add the ``:autosave`` option to the association:
13011307

13021308
class Band
13031309
include Mongoid::Document
1304-
1310+
13051311
has_many :albums
13061312
end
13071313

13081314
class Album
13091315
include Mongoid::Document
1310-
1316+
13111317
belongs_to :band, autosave: true
13121318
end
1313-
1319+
13141320
band = Band.new
13151321
album = Album.create!(band: band)
1316-
1322+
13171323
# The band is persisted at this point.
1318-
1324+
13191325
album.reload
1320-
1326+
13211327
album.band_id
13221328
# => BSON::ObjectId('62576b4b53aefe178b65b8e3')
1323-
1329+
13241330
album.band
13251331
# => #<Band _id: 62576b4b53aefe178b65b8e3, >
13261332

@@ -1339,14 +1345,14 @@ A non-exhaustive list of these operations is as follows:
13391345
- Assignment to the association:
13401346

13411347
.. code-block:: ruby
1342-
1348+
13431349
# Saves the band and the album.
13441350
band.albums = [Album.new]
13451351

13461352
- ``push``, ``<<``:
13471353

13481354
.. code-block:: ruby
1349-
1355+
13501356
band.albums << Album.new
13511357
band.albums.push(Album.new)
13521358

spec/mongoid/association/referenced/has_and_belongs_to_many_models.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class HabtmmAnimal
7070

7171
class HabtmmSchool
7272
include Mongoid::Document
73+
include Mongoid::Timestamps
7374

7475
has_and_belongs_to_many :students, class_name: 'HabtmmStudent'
7576

@@ -80,6 +81,7 @@ class HabtmmSchool
8081

8182
class HabtmmStudent
8283
include Mongoid::Document
84+
include Mongoid::Timestamps
8385

8486
has_and_belongs_to_many :schools, class_name: 'HabtmmSchool'
8587

spec/mongoid/association/referenced/has_and_belongs_to_many_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,4 +1076,29 @@ class OtherHasManyRightObject; end
10761076
expect(association.inverse_foreign_key_setter).to eq('has_many_left_object_ids=')
10771077
end
10781078
end
1079+
1080+
context "when adding an object to the association" do
1081+
let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
1082+
1083+
let(:update_time) do
1084+
Timecop.freeze(Time.at(Time.now.to_i) + 2)
1085+
end
1086+
1087+
after do
1088+
Timecop.return
1089+
end
1090+
1091+
let!(:school) { HabtmmSchool.create! }
1092+
let!(:student) { HabtmmStudent.create! }
1093+
1094+
before do
1095+
update_time
1096+
school.update(students: [student])
1097+
end
1098+
1099+
it "updates the updated at" do
1100+
pending "MONGOID-4953"
1101+
expect(school.updated_at).to eq(update_time)
1102+
end
1103+
end
10791104
end

spec/mongoid/association/referenced/has_many_models.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class HmmPet
4242

4343
class HmmSchool
4444
include Mongoid::Document
45+
include Mongoid::Timestamps
4546

4647
has_many :students, class_name: 'HmmStudent'
4748

@@ -51,8 +52,9 @@ class HmmSchool
5152

5253
class HmmStudent
5354
include Mongoid::Document
55+
include Mongoid::Timestamps
5456

55-
belongs_to :school, class_name: 'HmmSchool'
57+
belongs_to :school, class_name: 'HmmSchool', touch: true
5658

5759
field :name, type: String
5860
field :grade, type: Integer, default: 3

spec/mongoid/association/referenced/has_many_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "spec_helper"
4+
require_relative './has_many_models'
45

56
describe Mongoid::Association::Referenced::HasMany do
67

@@ -1245,4 +1246,28 @@ class OtherBelongingObject; end
12451246
expect(association.create_relation(owner, target)).to be_a(Array)
12461247
end
12471248
end
1249+
1250+
context "when adding an object to the association" do
1251+
let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
1252+
1253+
let(:update_time) do
1254+
Timecop.freeze(Time.at(Time.now.to_i) + 2)
1255+
end
1256+
1257+
after do
1258+
Timecop.return
1259+
end
1260+
1261+
let!(:school) { HmmSchool.create! }
1262+
let!(:student) { HmmStudent.create! }
1263+
1264+
before do
1265+
update_time
1266+
student.update(school: school)
1267+
end
1268+
1269+
it "updates the updated_at" do
1270+
expect(student.updated_at).to eq(update_time)
1271+
end
1272+
end
12481273
end

0 commit comments

Comments
 (0)