I am making a phoenix app with quizes etc. and would appreciate some advice on how to model the data. Initially I thought a list would suffice for the anwers but quickly realised that I won’t be able to store which of the answers are correct. I starded thinking and came up with couple of roadblocks/questions. The structures I thought could be used
Order is not important and I wouldn’t really worry about the access time as for one question there will be max 6 answers but multiple can be correct though.
defmodule Quiz do
use Ecto.Schema
schema "quizes" do
field :question, :string
field :correct_answers, {:array, :string}
field :wrong_answers, {:array, :string}
end
end
Alternatively you can consider using embed:
defmodule Quiz do
use Ecto.Schema
schema "quizes" do
field :question, :string
embeds_many :answers do
field :correct, :boolean
field :label, :string
end
end
end
Two lists have the downside of making it more tricky to order answers independent of if they’re correct or incorrect ones. I’d probably go with a list of maps.
For my first example you can always use Enum.shuffle/1 and in the second example we can add position field of type integer, so the order can be set by user creating specific quiz.
Two lists are faster to check if answer is correct answer in quiz.correct_answers (i.e. no need to loop all answers including wrong). However the embed scenario is more flexible (like above we can easily add custom positions).
Interesting, didn’t think of that. The order isn’t important so the shuffle will work for me. Added bonus would be different position for the correct answers for the same question every time. I quite like it.
answer83 was a typo. And yes, there can be multiple correct answers.
How would you describe it in your schema and what postgres field type would you opt in for?
@derek-zhou I like the list of maps as it would be easier in my inexperienced mind to mark the answers correct with a checkbox rather than separating them into two lists. It may also come in handy if I decided to id the answers.
I guess im gonna try both and see which one’s easier to live with. Will come back and resolve this when I’ve decided. All the help is much appreciated.
This is why I have suggested an alternative solution.
Once fetched from database the embeds_many field returns a list of maps. Also embedded data have their own ids by default, so the ecto knows if said answer is newly added or just updated. Of course nothing stops you from shuffling embedded list.
I appreciate it. Ecto is a whole new animal to me which I do not have full grasp on hence I was leaning towards staying off the embedded schema and anything I deem black magic at this point I read a little bit but need to dive deeper to fully comprehend and feel comfortable and confident with it.