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
Hello. I'm trying to tidy up some code for a SQL query builder. Presently, I manually assign the inputs' name and id fields and save it all as JSONB.
How are you distinguishing between types in the frontend? The example in the docs only shows the sms part, when the channel can be either sms or email.
And have you had any luck using polymorphic_embed_inputs_for for an {:array, PolymorphicEmbed}?
Given these schemas:
defmodulePolypipe.EmbedQuerydodefmoduleEmbedRuledouseEcto.SchemaimportEcto.Changesetembedded_schemadofield:field,:stringfield:operator,Ecto.Enum,values: ~w(lt le eq neq ge gt like ilike notlike notilike in notin null notnull)afield:value,:stringenddefchangeset(rule,params)dorule|>cast(params,[:field,:value,:operator])endenddefmoduleEmbedRuleGroupdouseEcto.SchemaimportEcto.ChangesetimportPolymorphicEmbed,only: [cast_polymorphic_embed: 3]aliasPolypipe.EmbedQuery.{EmbedRule,EmbedRuleGroup}embedded_schemadofield:condition,:stringfield:rules,{:array,PolymorphicEmbed},types: [rule_group: [module: EmbedRuleGroup,identify_by_fields: [:condition,:rules]],rule: [module: EmbedRule,identify_by_fields: [:field,:operator]]],on_type_not_found: :raise,on_replace: :deleteenddefchangeset(group,params)dogroup|>cast(params,[:condition])|>cast_polymorphic_embed(:rules,required: true)endenduseEcto.SchemaimportEcto.Changesetschema"embed_queries"dofield:name,:stringembeds_one:rule_group,__MODULE__.EmbedRuleGroup,on_replace: :deleteenddefchangeset(data\\%__MODULE__{},params)dodata|>cast(params,[:name])|>cast_embed(:rule_group)endend
And this basic template (haven't use components yet as I just wanted to see if it works).
This kind of works, but it clearly isn't right because I'm telling it these polymorphic embeds are rules, when really they're a list of rules and rule_groups.
The text was updated successfully, but these errors were encountered:
I think we tried using {:array, PolymorphicEmbed} before, and I don't remember all the details, but we ended up with a different schema structure: The main schema A has an embeds_many on a schema B. That schema B has some meta fields common to all polymorphic types, plus a data field that holds the actual polymorphic data. Something like:
defSchemaAdoschema"schema_a"do# more fieldsembeds_many:bs,SchemaB,on_replace: :deleteendenddefSchemaBdoembedded_schemado# more fieldsfield:data,PolymorphicEmbed,types: [type_a: TypeA,type_b: TypeB,],on_type_not_found: :raise,on_replace: :updateendend
And that works fine in combination with the polymorphic_embed_inputs_for/2 from the PR.
This schema structure might not be the best solution for all use cases, but I would imagine that it could work similarly with an array field. I haven't tried it, though.
The examples are a bit hastily extracted from a way more complex form, there might be some errors hidden in there. I hope that it still helps to give you some ideas. 😅
{:array, PolymorphicEmbed} works with polymorphic_embed_inputs_for. Maybe requires to tweak input names when working with lists, and lib might need improvements, not sure.
Hello. I'm trying to tidy up some code for a SQL query builder. Presently, I manually assign the inputs'
name
andid
fields and save it all as JSONB.How are you distinguishing between types in the frontend? The example in the docs only shows the
sms
part, when the channel can be eithersms
oremail
.And have you had any luck using
polymorphic_embed_inputs_for
for an{:array, PolymorphicEmbed}
?Given these schemas:
And this basic template (haven't use components yet as I just wanted to see if it works).
and this data
This kind of works, but it clearly isn't right because I'm telling it these polymorphic embeds are
rules
, when really they're a list ofrules
andrule_groups
.The text was updated successfully, but these errors were encountered: