shahryarjb
Hello, I have a struct that was made with preload, this output cannot be converted to json encode.
it should be noted, I can’t use @derive in my schema files, but I use it in my query file, but the task was broken , because I have multi struct in a list.
now how can I convert it to json, I’m using Jason lib,
my preload:
def show_support_with_code(code, user_id) do
query = from u in SupportSchema,
where: u.code == ^code,
join: a in assoc(u, :users),
# where: a.id == ^user_id,
left_join: b in assoc(u, :blog_posts),
left_join: c in assoc(u, :support_replies),
left_join: d in assoc(c, :users),
order_by: [desc: c.inserted_at],
preload: [users: a, blog_posts: b, support_replies: {c, users: d}]
case Repo.one(query) do
nil -> {:error, :show_support_with_code}
info -> {:ok, :show_support_with_code, info}
end
end
output preload:
%BankError.ClientApi.Support.SupportSchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "supports">,
blog_posts: nil,
code: "test47",
description: "بررسی بهینه سازی در اگهی",
id: "cd33f63f-44c5-46a7-848c-8926be21dd82",
inserted_at: ~N[2019-08-25 17:43:49],
post_id: nil,
status: 3,
support_replies: [
%BankError.Support.SupportReplySchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "support_replies">,
description: "برای تست اینجاییم",
id: "e67dae2f-237c-439f-bee7-846f8f575cc9",
inserted_at: ~N[2019-08-29 17:24:25],
status: true,
support_id: "cd33f63f-44c5-46a7-848c-8926be21dd82",
supports: #Ecto.Association.NotLoaded<association :supports is not loaded>,
updated_at: ~N[2019-08-29 17:24:26],
user_id: "40c06a6b-15a0-4888-999b-132fbd326455",
users: %BankError.Users.UserSchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: "40c06a6b-15a0-4888-999b-132fbd326455",
inserted_at: ~N[2019-05-26 08:12:14],
last_name: "soam",
mobile: "09213780329",
name: "shiam",
password: nil,
password_hash: "$2b$12$N2c6fKCVez7ZB/5k7coXkuwNaVGESQboFNba8cmRcRqoaKufs4PAC",
role: 1,
status: 1,
support_replies: #Ecto.Association.NotLoaded<association :support_replies is not loaded>,
supports: #Ecto.Association.NotLoaded<association :supports is not loaded>,
transactions: #Ecto.Association.NotLoaded<association :transactions is not loaded>,
updated_at: ~N[2019-05-26 08:12:14]
}
}
],
title: "ساختمان سازی یک تست دوم",
type: "other",
updated_at: ~N[2019-08-26 06:29:23],
user_id: "d0c1c8c5-6747-47c7-ba21-89a15017be3c",
users: %BankError.Users.UserSchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: "d0c1c8c5-6747-47c7-ba21-89a15017be3c",
inserted_at: ~N[2019-04-16 08:20:10],
last_name: "tavakkoli",
mobile: "09368094936",
name: "shahryar",
password: nil,
password_hash: "$2b$12$k/Ql7o9VyUbeeHi3Dr1BuOub2Wdg6NZ0I8bIfM8SmaqNop8o60H.G",
role: 3,
status: 2,
support_replies: #Ecto.Association.NotLoaded<association :support_replies is not loaded>,
supports: #Ecto.Association.NotLoaded<association :supports is not loaded>,
transactions: #Ecto.Association.NotLoaded<association :transactions is not loaded>,
updated_at: ~N[2019-08-26 05:38:18]
}
}
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
shahryarjb
I tested it
but the output is:
I can’t be able to delete
%BankError.Users.UserSchemaand some field like__meta__and etc inusersmaphelp me please
@peerreynders @OvermindDL1 @NobbZ
albydarned
Have you tried a simple
Map.from_structshahryarjb
I couldn’t use it, I cant delete all the field I need like (password_hash , support_replies,
__meta__, and etc.)I mean a loop should be created and find these keys and delete, but it just works in a single tuple like
%{name: 1, last_name: 2}not a list like my first topicalbydarned
Why?
albydarned
Sorry, I totally missed the part where you had a list in the struct
shahryarjb
At first, the struct created was used in many files and then I didint want to change my files I hadn’t written test, but I test @derive in my schema file, and it didnt work for me I dont know why ?!
like:
I added
@derivein other schema files like (support_replies,users)But it didn’t change
albydarned
Maybe a dumb suggestion, but I would make sure your project really is using
Jasonand notPoison.From the Jason docs you can also place the derive outside any module.
shahryarjb
I didn’t install
Poisonin my project I think phoenix 1.4 uses Jason default.I saw this
Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...]), but don’t know where I can use it, because I never have used Protocol before, but am reading the documentsThank you
albydarned
Hope that helps! Also, make sure when you add the @derive that your project recompiles.
shahryarjb
I used it in my controller.
and I have this error:
but if I change it to :
I mean delete
support_repliesit works but it doesn’t load other my tables that are called like
in my
Preloadquerymy schema: