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

Add ActiveModel::Attributes#==. #46517

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bonobono555
Copy link

@bonobono555 bonobono555 commented Nov 18, 2022

Motivation / Background

I want to be able to compare identity checks with == method. Because I want to compare attribute identities,
but currently I have to go through the attribute method.

Detail

# before
UserModel.new(id: 1) == UserModel.new(id: 1) # false

# after
UserModel.new(id: 1) == UserModel.new(id: 1) # true

Additional information

I wanted to use AttributeSet#== for the comparison to avoid creating unnecessary objects, but since it does not return true if the value before type cast are different, I used Attributes#attributes instead.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.
  • CI is passing.

@bonobono555
Copy link
Author

I'll fix it in lint later.

I want to be able to compare identity checks with == method.
Because I want to compare attribute identities,
but currently I have to go through the attribute method.

I wanted to use AttributeSet#== for the comparison to avoid creating
unnecessary objects, but since it does not return true if the value
before type cast are different, I used Attributes#attributes instead.

```ruby
# before
UserModel.new(id: 1) == UserModel.new(id: 1) # false

# after
UserModel.new(id: 1) == UserModel.new(id: 1) # true
```

*aaasa*
@bonobono555 bonobono555 force-pushed the add_equal_method_to_activemodel_attributes branch from 07da7dc to 9b73de4 Compare November 21, 2022 09:34
Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

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

Attributes should not know about equality. If we want to implement this we should do in ActiveModel::Model that is the collection of features from Active Model to make objects behave like models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants