You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It appears that enum when using custom values are completly broken. It always raises something like ArgumentError: invalid value 'fr-CA' is assigned
I tried debugging and it seems internally StoreModel::Types::EnumType#cast_integer_value(value) is called properly with 'fr' but then is called a second time with the already converted value (fr-ca)
Note: This happens on the record loading, record update, record assignation, etc.
Setting raise_on_invalid_values: false will get rid of the error but will persist the enum key in database instead of the value.
Here is a reproduction test
# frozen_string_literal: truerequire'bundler/inline'gemfile(true)dosource'https://rubygems.org'gem'rails'gem'minitest-rails'gem'pg'gem'store_model'endrequire'active_record'require'minitest/autorun'require'logger'ActiveRecord::Base.establish_connection(adapter: 'postgresql',database: 'test',host: 'localhost',port: 5437,username: 'dev',password: 'dev',query_cache: false)ActiveRecord::Base.logger=Logger.new(nil)ActiveRecord::Schema.definedocreate_table'users',force: :cascadedo |t|
t.string:namet.jsonb:organizationendendclassOrganizationincludeStoreModel::Modelattribute:id,:integer# attribute :locale, :stringenum:locale,in: {en: "en-CA",fr: "fr-CA"}endclassUser < ActiveRecord::BaseincludeStoreModel::NestedAttributesattribute:organization,Organization.to_typeaccepts_nested_attributes_for:organization,reject_if: :all_blank,update_only: trueenddescribe"StoreModel Bug"dodeftest_organization_with_valid_locale_should_be_loadableActiveRecord::Base.connection.execute(<<-SQL) INSERT INTO users (id, name, organization) VALUES (1, 'John Doe', '{ "id": 1, "locale": "fr-CA" }'); SQLdb_locale=ActiveRecord::Base.connection.execute(<<-SQL) SELECT organization->>'locale' FROM users WHERE id = 1; SQLassert_equal"fr-CA",db_locale.first.values.firstuser=User.find(1)assert_equal"fr",user.organization.localeenddeftest_it_updates_localeuser=User.create!(name: "John Doe",organization: {id: 1,locale: :fr})assert_equal"fr",user.organization.localeuser.organization.locale=:enuser.save!db_locale=ActiveRecord::Base.connection.execute(<<-SQL) SELECT organization->>'locale' FROM users WHERE id = 1; SQLassert_equal"en",user.organization.localeassert_equal"en-CA",db_locale.first.values.firstendend
The text was updated successfully, but these errors were encountered:
Hi,
It appears that
enum
when using custom values are completly broken. It always raises something likeArgumentError: invalid value 'fr-CA' is assigned
I tried debugging and it seems internally
StoreModel::Types::EnumType#cast_integer_value(value)
is called properly with 'fr' but then is called a second time with the already converted value (fr-ca)Note: This happens on the record loading, record update, record assignation, etc.
Setting
raise_on_invalid_values: false
will get rid of the error but will persist the enum key in database instead of the value.Here is a reproduction test
The text was updated successfully, but these errors were encountered: