shahryarjb
How to get struct in array
Hello, how do I get struct in array ? please see my pic :
I need to get the struct’s params , like this : a.cms_post.title
My output is :
[
%TrangellCmsService.Cms.Db.Post{
__meta__: #Ecto.Schema.Metadata<:loaded, "cms_post">,
changelog: true,
changelog_category: "/ss/ppos",
cms_post_category: #Ecto.Association.NotLoaded<association :cms_post_category is not loaded>,
cms_post_category_id: "960c30f2-183e-4430-a96d-4f0fd41fe37b",
description: "test test test test test test tetststststss tskkkstest of test fo test of test more test more test for test for test tes",
discourse: true,
discourse_link: "/ss/ppos",
download_ext_link: "/ss/ppos",
group_acl: "admin",
id: 6,
inserted_at: ~N[2018-04-29 11:45:05.311436],
learn: true,
learn_category: "/ss/ppos",
pic_x1_link: "/ss/ppos",
pic_x2_link: "/ss/ppos",
pic_x3_link: "/ss/ppos",
plugin: true,
plugin_category: "/ss/ppos",
post_type: "free",
price: "30000",
screen_shot: false,
screen_shot_category: "/ss/ppos",
seo_alias_link: "7seo-why-seo",
seo_description: "for the test for test for test for test tete of test",
seo_language: "fa",
seo_language_link: "/ss/ppos",
seo_tag: "test,test,test,test",
seo_words: "test,test,test,test",
status: true,
title: "shahryar",
updated_at: ~N[2018-04-29 11:45:05.311884]
}
]
Marked As Solved
Eiji
@shahryarjb: I assumed you want to fetch online titles. If so then easier is to call specific query.
defmodule Example do
import Ecto.Query
# aliases for Post and PostCategory here
def sample(seo_alias_link, group_acl) do
query =
from(
post_category in PostCategory,
join: post in assoc(post_category, :cms_post)
order_by: post_category.inserted_at,
limit: 10,
preload: [cms_post: post],
where: p.seo_alias_link == ^seo_alias_link
)
if group_acl == "admin" do
query
else
where(
query,
[post_category, post],
post_category.group_acl == ^group_acl and post.group_acl == ^group_acl
)
end
query
|> Repo.all()
|> Enum.map(& &1.cms_post)
|> List.flatten()
|> Enum.map(& &1.title)
end
end
Example.sample("jsjsjs", "admin")
1
Also Liked
peerreynders
Something like
Enum.map(a.cms_post, &(Map.get(&1,:title)))
would return a list of title values
a.cms_post
|> hd()
|> Map.get(:title)
returns the title of the post in the head of the list
def get_title([%TrangellCmsService.Cms.Db.Post{title: title}|_]),
do: title
i.e. using pattern matching for destructuring rather than dot notation.
3
NobbZ
Not array, it is a linked list, and you can use the usual iteration functions from the Enum module.
You can not directly acces the fields because it’s ambiguous which of the potentially many lists contents you want to access.
2
Eiji
@shahryarjb If I understand you correctly then this code should help you:
categories # here you have root list from your gist
|> Enum.find(& &1.seo_alias_link == "jsjsjs") # here you have list element
|> Map.fetch!(:cms_post) # here you are fetching sub-list from element
|> Enum.map(& &1.title) # finally here you are getting title for all elements
1
Popular in Questions
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
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
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
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
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Other popular topics
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
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New









