trenton

trenton

Hello folks,

I am currently trying to take an object and return some data on it via an endpoint. This object, an account, has many account opportinities and I am running into this error.

protocol Enumerable not implemented for #Ecto.Association.NotLoaded<association :account_opportunities is not loaded> of type Ecto.Association.NotLoaded (a struct). This protocol is implemented for the following type(s): Ecto.Adapters.SQL.Stream, Postgrex.Stream, DBConnection.Stream, DBConnection.PrepareStream, Scrivener.Page, KafkaEx.Stream, HashSet, Range, Map, Function, List, Stream, Date.Range, HashDict, GenEvent.Stream, MapSet, File.Stream, IO.Stream

This is the method the endpoint hits.

def accounts_by_owner(conn, %{"owner_id" => id, "email" => email}) do
    result = 
      Data.get_account_by_email(email, id)
      |> Enum.map(fn account -> 
        CrmManagerWeb.AccountView.render("account_with_opportunities.json", account: account) end)

    json(conn, %{accounts: result})
  end

I am trying to map it to this json view.

def render("account_with_opportunities.json", %{account: account}) do
    %{
      crm_type: account.crm_type,
      crm_id: account.crm_id,
      match_source: account.match_source,
      opportunities:
        render_many(
          account.account_opportunities,
          CrmManagerWeb.AccountOpportunityView,
          "slim_account_opportunity.json"
        )
    }
    |> Map.merge(account.other_data || %{})
  end

It throws this error when trying to render many. I’m not entirely sure what it going on here. The query that I am running to get these accounts returns something like this…

[       
  %CrmManager.Data.Account{
    __meta__: #Ecto.Schema.Metadata<:loaded, "accounts">,
    account_dsm_ids: #Ecto.Association.NotLoaded<association :account_dsm_ids is not loaded>,
    account_opportunities: #Ecto.Association.NotLoaded<association :account_opportunities is not loaded>,
    account_owner_email: nil,
    contacts: #Ecto.Association.NotLoaded<association :contacts is not loaded>,
    crm_id: "TEST-A",
    crm_type: :sf,
    id: 33,
    inserted_at: ~N[2019-02-06 15:37:16],
    match_source: :integration,
    other_data: %{},
    owner_id: "12345",
    updated_at: ~N[2019-02-06 15:37:16]
  },
  %CrmManager.Data.Account{
    __meta__: #Ecto.Schema.Metadata<:loaded, "accounts">,
    account_dsm_ids: #Ecto.Association.NotLoaded<association :account_dsm_ids is not loaded>,
    account_opportunities: #Ecto.Association.NotLoaded<association :account_opportunities is not loaded>,
    account_owner_email: nil,
    contacts: #Ecto.Association.NotLoaded<association :contacts is not loaded>,
    crm_id: "TEST-B",
    crm_type: :salesforce,
    id: 34,
    inserted_at: ~N[2019-02-06 16:19:21],
    match_source: :integration,
    other_data: %{},
    owner_id: "12345",
    updated_at: ~N[2019-02-06 16:19:21]
  }
]

Any ideas what could be going on here? Perhaps I need to preload the associations?

Showing Posts 1 to 1

A-Legg

A-Legg

The exception you are receiving stems from the fact that you aren’t preloading the associated account opportunities in the account.

— All posts loaded —

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

Other Trending Topics Top

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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews