HerrBertling
Nested data structures the Phoenix way?
Hey all!
I‘m currently learning Phoenix and Elixir. My day job is working on the frontend side of things with React.
For fun, I started building a project management app for craftsmen. Different companies should use it. They‘d have projects, a calendar, overview of the latest changes,…
There’s a nested data structure that I have on the project detail page and I‘d love to get some advice on how to „think“ of this in a Phoenix-Elixir-way.
Each project has phases, each phase has tasks and each task can have appointments. I‘ve got all of these in separate data tables (another thing I never touched - data modeling on the db level
) which seems to be the correct way to do it, quite a few belongs_to in there as well.
When I think about this from the UI side of things, my hunch is to add a form for „add task“ as a LiveComponent. I could of course do all of this at once in a single LiveView and render some nested data structure with different events for different forms. Or could I? Coming from Remix, I‘m used to thinking in nested routes, but that‘s unfortunately not a thing in Phoenix from what I understood so far.
Since I have almost no experience with all of this besides a few tutorials, I would love to get some feedback on how people handle these scenarios. Nested data structures have to be common scenarios, no?
Appreciate any thoughts and hints on this.
Most Liked
josevalim
Welcome @HerrBertling! Generally speaking, you may read data as nested but most updates happen within the data directly.
For example, if projects has phases, phases has tasks, and task have appointments, and you are showing them all on the same page, then you would likely load all of the data as nested, using Ecto’s preloads. On the other hand, if you only show appointments once you are seeing a task page, which is potentially a separate LiveView, you are likely only loading that particular task and its appointments.
When it comes to adding a new task, you most likely send only the “phase_id” to the server alongside the task parameters. Then when you receive the event, you validate the user can indeed edit and add tasks to that phase, and then you insert the task with its relevant phase_id to the database. The insertion does not need to go through the nesting.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









