attr_json
attr_json copied to clipboard
Work with rails enums?
Thank you for this very useful and well documented gem. Does it work with rails enums?
I tried:
class UserRegistration
include AttrJson::Record
attr_json :first_name, :string
attr_json :last_name, :string
attr_json :status, :integer
enum :status, [ :new, :processed ], prefix: true
end
It kind or works, model instance methods such as status_new!
do not throw errors, but the enum values do not get persisted in the jsonb database field (under the :status key).
I'm not sure if I've ever tried this!
Sorry, I'm not sure what is going wrong. And I am not sure when I will have time to investigate. If you'd like to do more investigation and report back, with ideas for a fix, that would be very helpful!
I am not really sure if it would be feasible to support or not, to be honest!
Thank you, currently I do not have time to investigate this further. But I am using this gem anyway and it works well so far. The enum feature is kind of a comfort feature and I can work around it. I will close this issue for now, hoping for more time in the future.
Hi, I noticed something that sounds relevant in the Rails 7.1.2 release notes, although it frustratingly doesn't include a link to a PR or commit; I haven't yet tracked down the details.
Re-enable support for using enum with non-column-backed attributes. Non-column-backed attributes must be previously declared with an explicit type. For example:
class Post < ActiveRecord::Base
attribute :topic, :string
enum topic: %i[science tech engineering math]
end
https://github.com/rails/rails/releases/tag/v7.1.2
@pcdoc80 could you say what version of Rails you were using where it didn't work? If it was 7.1.0 or 7.1.1, could you try again with 7.1.2?