Sometimes minor updates add the most joy to users!
A recent Rails update introduced the ability to monitor for changes in belongs_to
associations.
This brings in a welcome change for developers who like to conform to “the Rails way”.
Change tracking is one of the convenient methods that Rails supports out of the box. When using ActiveRecord, we can query for database changes. This is useful when building triggers and events. Let’s look at an example!
Imagine a database structure that has posts and categories.
Now let’s say that we want to record activity when the posts’ title gets changed. We could write something that looks like this,
The title_changed?
allows us to monitor any changes to the value of the title
attribute.
Before
Now, let’s try to do the same thing when a posts’ category changes.
This is an eyesore!
We have an association called category
,
but we can’t monitor for its change.
Rather,
we have to resort to a rather crude way of monitoring for category_id
changes.
This isn’t very “the Rails way”.
After
Fortunately, Rails 7 now introduces a way to do this. With this PR, we can now do this.
The association_changed?
method (assuming an association named :association
) returns true if a different associated object has been assigned and,
the foreign key will be updated in the next save!
We also get access to this method,
association_previously_changed?
which returns true if the previous save updated the association to reference a different associated object.