Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not pollute model changes on initialisation #88

Merged
merged 2 commits into from
Oct 20, 2020

Conversation

bliof-fc
Copy link
Member

@bliof-fc bliof-fc commented Oct 19, 2020

This is done as in rails >= 5.2 the lock! & with_lock methods now
require that the record doesn't have any changes otherwise they'll raise
an error.

https://github.com/rails/rails/blob/v5.2.4.4/activerecord/lib/active_record/locking/pessimistic.rb#L65

      def lock!(lock = true)
        if persisted?
          if has_changes_to_save?
            raise(<<-MSG.squish)
              Locking a record with unpersisted changes is not supported. Use
              `save` to persist the changes, or `reload` to discard them
              explicitly.
            MSG
          end

          reload(lock: lock)
        end
        self
      end

Apparently clear_attribute_changes receives an array of attributes which
it should clear.

https://apidock.com/rails/v5.2.3/ActiveModel/Dirty/clear_attribute_changes

This is done as in rails >= 5.2 the lock! & with_lock methods now
require that the record doesn't have any changes otherwise they'll raise
an error.

https://github.com/rails/rails/blob/v5.2.4.4/activerecord/lib/active_record/locking/pessimistic.rb#L65

```
      def lock!(lock = true)
        if persisted?
          if has_changes_to_save?
            raise(<<-MSG.squish)
              Locking a record with unpersisted changes is not supported. Use
              `save` to persist the changes, or `reload` to discard them
              explicitly.
            MSG
          end

          reload(lock: lock)
        end
        self
      end
```

Apparently clear_attribute_changes receives an array of attributes which
it should clear.

https://apidock.com/rails/v5.2.3/ActiveModel/Dirty/clear_attribute_changes
Copy link

@denislavski denislavski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@bliof-fc bliof-fc merged commit 887e708 into master Oct 20, 2020
@bliof-fc bliof-fc deleted the do-not-pollute-model-changes branch October 20, 2020 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants