nikolis

nikolis

Working with many to many relationship though user input(Form)

So I have being playing around with an app to better digest elixir and I have come across a tough choice.

In my user case scenario there a user can create he’s own user meal, this meal can have multiple food recipes and as it seem natural a food recipe can be part of many user meals. So how I am handling this is through a many to many association in the db layer.

UserMeal <-(RecipeUserMeal)-> Recipe

so in order to create the recipe_user_meals field of the UserMeal automatically when perssisting a UserMeal Struct I would need something like [%{recipe_id: 1} , %{recipe_id: 2}] etc ..

How I am handling that in the form creating the UserMeal I have a multiple_select so that the user can select multiple recipes if needed to associate with the particular UserMeal but the multiple_select is actually producing an array of ids like ["1", "2"] and how I am currently handling that is using a tranformation function. to convert the multiple_select content (["1", "2"]) to something the ecto can understand
([%{recipe_id: 1} , %{recipe_id: 2}]). but this converertion that needs to happen one way in whenever I am having a direction from live_view form to ecto and the other way when I am going from ecto content to live_view form kind makes my code fuzzy and difficult to understand.

So to those of you that have come across anything similar
Do you have any suggestion for me ?
Could it be that should I create my own multiple_select building on top of the Phoenix.HTML.Form one so that it feets my needs in particular ?
Am I missing something really obvious even in the db model layer that could make my life easier ?

First Post! Switch mode

evadne

evadne

Would suggest reviewing Ecto virtual fields, perhaps you can find an opportunity there for simplification. Consider using an Ecto schema just for form interoperation and roll it to/from the base model that can be used with the database with additional code

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement