Hey
I’ve got a little problem with encoding my structs as json with Poison. I have a module called Task which can have other Tasks as dependencies, looking like this:
@derive {Poison.Encoder, only: [:name, :dependencies, :costs]}
schema "task" do
field :name, :string
has_many :costs, App.Cost
has_many :dependencies, App.Dependency, on_replace: :delete
end
@derive {Poison.Encoder, only: [:value, :task_dependency]}
schema "dependency" do
field :value, :integer
belongs_to :task, App.Task
belongs_to :task_dependency, App.Task
end
In my frontend I want to show Tasks and their corresponding Task, which they are dependent on. So I take all my tasks and preload(task_dependencies: :task_dependency) and preload(:costs).
On my local machine there is no error but if I push this to Heroku the Application crashes at this point with
"** (RuntimeError) cannot encode association :dependencies from App.Task to JSON because the association was not loaded.
because @derive says to encode ‘costs’ of the associated tasks.
So, my solution at this point is to manually Map.drop(:costs) from the associated task to bypass this, but it leads to other problems later on.
So, is there any smarter solution to this or is it even a horrible idea to associate a struct to its own module?