I am encountering an issue while using Enumerize with PostgreSQL, and would appreciate your guidance in resolving it. The details of the issue are as follows:
Bug:
When an Enumerize attribute, which holds a boolean value in PostgreSQL, is set to false, a SQL error occurs upon saving.
System Details:
DBMS: PostgreSQL 13.6
Ruby: 3.2.2
Rails: 7.0.6
Enumerize: 2.7.0
Steps to Reproduce:
# ActiveRecord
class User < ActiveRecord::Base
enumerize :newsletter_subscribed, in: { subscribed: true, unsubscribed: false }
end
create_table "users", force: :cascade do |t|
t.boolean "newsletter_subscribed", default: true, null: false
end
User.create!(newsletter_subscribed: true) # Works when set to true
User.create! # Works because the default value is true
User.create!(newsletter_subscribed: false) # Error when set to false
Details:
The error only occurs when the value is false. Here are the executed SQL statements:
When true:
INSERT INTO "users" ("newsletter_subscribed") VALUES ($1) RETURNING "id" [["newsletter_subscribed", true]]
When false:
INSERT INTO "users" ("newsletter_subscribed") VALUES ($1) RETURNING "id" [["newsletter_subscribed", "unsubscribed"]]
As seen above, the value isn't serialized to the DB-specific value (false) when unsubscribed (false).
Hello,
I am encountering an issue while using Enumerize with PostgreSQL, and would appreciate your guidance in resolving it. The details of the issue are as follows:
Bug:
When an Enumerize attribute, which holds a boolean value in PostgreSQL, is set to false, a SQL error occurs upon saving.
System Details:
Steps to Reproduce:
Details:
The error only occurs when the value is false. Here are the executed SQL statements:
As seen above, the value isn't serialized to the DB-specific value (false) when
unsubscribed
(false).This error seems to have started after the merge of PR: https://github.com/brainspec/enumerize/pull/424, and it appears to be caused by the
cast
method. Although I'm not an expert on this gem, renaming thecast
method seems to make it work as expected anyway, suggesting that the naming could be the issue. Interestingly, ActiveModel also has a method with the same name, which may or may not be related: https://github.com/rails/rails/blob/v7.0.6/activemodel/lib/active_model/type/value.rb#L45-L47I look forward to your feedback on this issue.
Best Regards