tombraul
How to build a survey app the Phoenix way?
Hola Elixir community.
We are currently thinking about switching to Elixir and Phoenix (with Liveview) at our company. Currently we are heavily using Next.js and Laravel. One question that is still open is how we would implement the data layer for our survey app. We have different kind of question types like simple select questions, grid/matrix questions, open input questions and more complex questions like a shopping simulator. Each of those question types have different kinds of properties, ui views and response types. Each survey as many questions while each question can be a different type of question.
In next.js we would just store the question information in a json field and work with interfaces and types in the frontend.
With Laravel we would probably choose something like table inheritance.
What would be the “Phoenix way” of solving this problem? I know there are probably more than one solution, but what is your experience with existing Phoenix apps?
Most Liked
josevalim
Welcome @tombraul! One thing about Ecto, which is Elixir’s default tool for integrating with databases, is that it tries to stay close to the database. Therefore, your question can be generalized to “what is the best way to model my problem at the database level”.
If you haven’t done this before, it may require getting used to, but it is totally worth it as you won’t have impedance mismatch between your app and the database in the long term.
LostKobrakai
From my experience the biggest thing people struggle with when building survey services is the differenciation between a survey and a survey response.
When rendering the form for creating a survey response a survey record defines what fields appear in the form, but you wouldn’t use a Survey changeset with the form. It would need to be a SurveyResponse changeset, because that’s what is getting created (or updated). The survey record might also be an input for a SurveyResponse changeset to validate any submitted inputs.
So for the survey response form handling a survey record will be a parameter, but the form is not about the survey record.
Generally I’d suggest starting without a survey record, hardcoding the markup for an example survey. Then hardcode details about a survey for that route, but dynamically rendering the markup based on that hardcoded data and from there it should be simple to use a survey record loaded from the db instead of hardcoded data.
al2o3cr
A package that can help with this kind of approach is polymorphic_embed - it can provide some structure beyond just “a JSON field”.
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









