-
Notifications
You must be signed in to change notification settings - Fork 96
Add Rails true enum support #163
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I didn't have time to add documentation just yet. Happy to do it if the PR looks good.
@bgvo thanks. This looks great. Can you fix the lint issues. Then let's see if all tests are green.
@bgvo
bgvo
force-pushed
the
mimic_rails_enum_behavior
branch
from
May 24, 2023 16:06
a382bfd
to
5d8d5fd
Compare
@haffla Thank you for the excellent work. Lint issue fixed!
@bgvo can you please remove the change in the schema.rb
? It seems to break the test suite and is unrelated to this PR.
@bgvo
bgvo
force-pushed
the
mimic_rails_enum_behavior
branch
from
May 29, 2023 09:42
5d8d5fd
to
4b41545
Compare
@haffla Done. Sorry about that.
haffla
haffla
approved these changes
May 29, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
I first found this thread and gave Rails enum a shot. It "seemed" to work, but it only did for
String
mappings. Even ifenum
was defined, when assigning attributes,jsonb_accessor
would update the jsonb with the value from the public getter, which returns the corresponding enum key (whatever that is) and not the actual value that should be stored in the database.This was first noticed by kg-currenxie.
Example
Given:
MyClass.create(my_enum_attribute: "Foo")
will store a jsonb with"Foo"
as value instead of1
.With the introduced changes, the actual value stored will be
1
.The use of enums with integers allows a ton of flexibility when it comes to altering existing enum keys. I'm in the middle of a migration and having enums defined with integers instead of strings makes things much easier.