archimondain

archimondain

How to automatically get an empty list when encoding in Jason schema with unloaded associations?

Suppose I have the following:

defmodule Proj.Lecture do
    schema "lectures" do
       field :title, :string
       field :is_active?, :boolean
       has_many :list_courses, Proj.Course
    end
end

require Protocol
Protocol.derive(Jason.Encoder,
                Syntax.Lectures.Lecture,
                only: [:title, :is_active?,:list_courses,:id])

Suppose an object Proj.Lecture is loaded from the database without the :list_courses attribute loaded. I would like the Jason encoding to give an empty list in this case.

Is there a way to do that ? (with Jason or Poison).

I currently get an exception :
** (RuntimeError) cannot encode association :list_courses from Proj.Lecture to JSON because the association was not loaded.

Most Liked

LostKobrakai

LostKobrakai

You should have some place (e.g. a phoenix.view or a function) which applies that assumption. What if later there’s a part in your app which expects courses to be loaded. If you add your logic globally to the json encoding your new code would silently fail.

LostKobrakai

LostKobrakai

If there’s a decision to be made I don’t really see why it would be boilerplate. Associations not being loaded can just as well be an accident as it can be expected, which is why I feel having an explicit place to handle the conversion of unloaded assocs to an empty list is better then just globally expecting unloaded associations are always expected. If you put the logic in a function this would be one additional function call at the places you encode json data.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement