Skip to content

Commit 3cd6885

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 19ef4c1 commit 3cd6885

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

source/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

0 commit comments

Comments
 (0)