shahryarjb

shahryarjb

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]
  }
]

Showing Posts 13 to 4

Eiji

Eiji

@shahryarjb: In my previous answer &Example.sample/2 returns query. Calling &Repo.all/1 on such result should return already list of titles, so you do not need to work on lists and structs to fetch them manually. I think that such query-only way is easier.

shahryarjb

shahryarjb OP

What does it have effect in performance between regular join and preload? just preload is easier or something more?

Thank you for giving me your time.

Eiji

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")
shahryarjb

shahryarjb OP

I know this , but I want to use preload, without preload is simple and very clear, so why was preload made , really? and where I use this ?

Eiji

Eiji

@shahryarjb: Then it’s really simple. Assuming you want only list of titles:

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,
        inner_join: post in assoc(post_category, :cms_post),
        order_by: post_category.inserted_at,
        limit: 10,
        select: post.title,
        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
  end
end

"jsjsjs" |> Example.sample("admin") |> Repo.all
shahryarjb

shahryarjb OP

I am sorry for this and thank you again,

the Category_alias is unique slug link which indexed in my database, for this purpose my client searches with it. and group_acl is user’s access,

At the end, fetch_posts_by_category_alias is :

	defp fetch_posts_by_category_alias(seo_alias_link, "admin") do
	  Repo.all from p in PostCategory,
	           join: c in assoc(p, :cms_post),
	           where: p.seo_alias_link == ^seo_alias_link,
	           order_by: p.inserted_at,
	           limit: 10,
	           preload: [cms_post: c]
	end

	defp fetch_posts_by_category_alias(seo_alias_link, group_acl) do
	  Repo.all from p in PostCategory,
	           join: c in assoc(p, :cms_post),
	           where: p.seo_alias_link == ^seo_alias_link,
	           where: p.group_acl == ^group_acl,
	           where: c.group_acl == ^group_acl,
	           order_by: p.inserted_at,
	           limit: 10,
	           preload: [cms_post: c]
	end

My problem started since I used preload , preload doesn’t let me filter my output at first.

my all codes :

Eiji

Eiji

@shahryarjb: Sorry, but I don’t know few things:

  1. What’s value of category_alias variable - "jsjsjs" ?
  2. What’s value of group_acl variable - root list from gist ?
  3. fetch_posts_by_category_alias function body

If I assumed correctly (about your variables) then:

def load_posts_by_category_alias(category_alias, group_acl) do
  group_acl
  |> Enum.find(& &1.seo_alias_link == category_alias)
  |> Map.fetch!(:cms_post)
  |> Enum.map(& &1.title)
end
shahryarjb

shahryarjb OP

Hello @Eiji , I edited the code to improve my project , I think my way isn’t right, Do you think like me?

def load_posts_by_category_alias(category_alias, group_acl) do
		category = fetch_posts_by_category_alias(category_alias, group_acl)
		|> Enum.find(& &1.seo_alias_link == "#{category_alias}")

		post = category
		|> Map.fetch!(:cms_post)
		
		for %TrangellCmsService.Cms.Db.Post{} = p <- post do
			%{post_id: p.id, category_title: category.title}
		end
	end
Eiji

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
shahryarjb

shahryarjb OP

Hello @peerreynders , thank you ,

I was trying very much, but I couldn’t use Map,Enum in my original outputs

my original outputs : data.ex · GitHub

the post variable which I linked in github gist

	def load_posts_by_category_alias(category_alias, group_acl) do
		post = fetch_posts_by_category_alias(category_alias, group_acl)

		for p <- post do
			Enum.map(p, &(Map.get(&1,:title)))
		end
	end

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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 Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews