3

Add change tracking methods for `belongs_to` associations by georgeclaghorn · Pu...

 3 years ago
source link: https://github.com/rails/rails/pull/42751
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Contributor

georgeclaghorn commented 14 days ago

edited

Permit checking whether a belongs_to association has been pointed to a new target record in the previous save and whether it will point to a new target record in the next save.

post.category # => #<Category id: 1, name: "Ruby">

post.category = Category.second   # => #<Category id: 2, name: "Programming">
post.category_changed?            # => true
post.category_previously_changed? # => false

post.save!

post.category_changed?            # => false
post.category_previously_changed? # => true

Example use case: use Hotwire to broadcast removal of a blog post from its previous category when its category changes. Requires an accessor for the previous target of the association, which can be added separately.

To-do:

  • Documentation
  • Tests
  • Changelog entry

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK