Thanks @alassek.
You are right. Your solution works…
attribute :cost_center_ids, Types::Array.of(Types::String),
read: Types::Coercible::Array
…but it requires that you fill the array with Sequel.pg_array.
factory[:user, cost_center_ids: Sequel.pg_array(%w[1 2 3])]
I also need the option to set the array to NULL, …
factory[:user, cost_center_ids: nil] # results in empty array in db
…but this is not possible with this approach, it’s an empty array. Here dry-rb - dry-types v1.2 - Maybe could help, but I haven’t figured out how it works yet.
My goal is generally to work with nil and ruby arrays. The only purpose is to get a nil array as an empty ruby array when reading the record.
Another thing I noticed:
I also wanted to use the Types that are supplied with Hanami first.
# frozen_string_literal: true
require "dry/types"
module MyApp
Types = Dry.Types
module Types
# Define your custom types here
end
end
but when I use this Types at your example, a different behavior happens.
attribute :cost_center_ids, MyApp::Types::Array.of(Types::String),
read: MyApp::Types::Coercible::Array
I get
Failure/Error: subject { factory[:user, cost_center_ids: Sequel.pg_array(%w[1 2 3])] }
Dry::Types::SchemaError:
["1", "2", "3"] (Sequel::Postgres::PGArray) has invalid type for :cost_center_ids violates constraints (type?(Array, ["1", "2", "3"]) failed)
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/schema.rb:332:in `rescue in block in resolve_unsafe'
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/schema.rb:329:in `block in resolve_unsafe'
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/schema.rb:324:in `each'
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/schema.rb:324:in `resolve_unsafe'
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/schema.rb:60:in `call_unsafe'
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/constructor.rb:81:in `call_unsafe'
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/type.rb:47:in `call'
# /usr/local/bundle/gems/rom-sql-3.6.4/lib/rom/sql/commands/create.rb:26:in `block in execute'
# /usr/local/bundle/gems/rom-sql-3.6.4/lib/rom/sql/commands/create.rb:66:in `block in with_input_tuples'
# /usr/local/bundle/gems/rom-sql-3.6.4/lib/rom/sql/commands/create.rb:66:in `map'
# /usr/local/bundle/gems/rom-sql-3.6.4/lib/rom/sql/commands/create.rb:66:in `each'
# /usr/local/bundle/gems/rom-sql-3.6.4/lib/rom/sql/commands/create.rb:66:in `with_input_tuples'
# /usr/local/bundle/gems/rom-sql-3.6.4/lib/rom/sql/commands/create.rb:25:in `execute'
# /usr/local/bundle/gems/rom-core-5.3.2/lib/rom/command.rb:278:in `call'
# /usr/local/bundle/gems/rom-sql-3.6.4/lib/rom/sql/commands/error_wrapper.rb:18:in `call'
# /usr/local/bundle/gems/rom-core-5.3.2/lib/rom/commands/composite.rb:19:in `call'
# /usr/local/bundle/gems/rom-factory-0.12.0/lib/rom/factory/builder/persistable.rb:48:in `persist'
# /usr/local/bundle/gems/rom-factory-0.12.0/lib/rom/factory/builder/persistable.rb:28:in `create'
# /usr/local/bundle/gems/rom-factory-0.12.0/lib/rom/factory/factories.rb:171:in `[]'
# ./spec/slices/auth/structs/user_spec.rb:43:in `block (4 levels) in <top (required)>'
# ./spec/slices/auth/structs/user_spec.rb:46:in `block (4 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# Dry::Types::ConstraintError:
# ["1", "2", "3"] violates constraints (type?(Array, ["1", "2", "3"]) failed)
# /usr/local/bundle/gems/dry-types-1.7.2/lib/dry/types/constrained.rb:37:in `call_unsafe'
for this line of code:
factory[:user, cost_center_ids: Sequel.pg_array(%w[1 2 3])