John_Shelby

John_Shelby

List as a reference to the same schema

Hi :slight_smile: ,

Can I have a reference using the list of ids to that schema where the current reference is?

The way the application currently works is that a question can have an ancestor with a reference via id, and based on the answer in it, it is displayed. I now need to store a list of question ids using multi select instead of question id, and the ids from the list should reference the selected ancestors.

What should the given reference look like please, if it is possible to create it? So far my experience is only with the basic reference type and I can’t find a solution, thank you.

Current code with reference through one id:

Schema:

schema "assessment_questions" do
    field :question, :string
    field :temp_id, :string, virtual: true
.
.
.
    belongs_to :ancestor_question,
               MyApp.AssessmentPipelines.AssessmentPipeline.AssessmentQuestion

    timestamps()
  end

  def changeset(assessment_question, attrs) do
    assessment_question
    |> Map.put(:temp_id, assessment_question.temp_id || attrs["temp_id"])
    |> cast(attrs, [
      :question,
.
.
.
      :ancestor_question_id
    ]) 
.
.
.
  end

Migration

  def change do
    alter table("assessment_questions") do
      add :ancestor_question_id,
          references(:assessment_questions, on_delete: :nothing, type: :binary_id)
    end
  end

Thanks for any help.

Marked As Solved

al2o3cr

al2o3cr

What does the result you’re looking for look like in plain SQL? It can be easier to put together the necessary Ecto parts if you know where you’re heading.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement