Accessing the values from the resultset in ecto

Sorry for the stupid question. :grinning:

My result set looks like this

   __meta__: #Ecto.Schema.Metadata<:loaded, "transactions">,
   amount_paid: 0.0,
   bill_amount: 0.0,
   comments: nil,
   created_by: "shan",
   customer_id: "shan",
   customers: #Ecto.Association.NotLoaded<association :customers is not loaded>,
   discount_on_items: 0.0,
   discount_on_total: 0.0,
   inserted_at: #DateTime<2019-01-30 08:46:21Z>,
   is_active: true,
   net_amount: 0.0,
   sale_date: #DateTime<2019-01-30 08:46:21Z>,
   sales_type: "COUNTER_SALE",
   tax: 0.0,
   tax_percentage: "0%",
   transaction_status: "PENDING",
   transactions_id: 1548837981076,
   updated_at: #DateTime<2019-01-30 08:46:21Z>,
   updated_by: "shan"
   __meta__: #Ecto.Schema.Metadata<:loaded, "transaction_details">,
   cost_price: 100.0,
   created_by: "shan",
   discount: 4.4,
   inserted_at: #DateTime<2019-01-30 08:46:32Z>,
   price: 1306.0,
   product_id: "A3",
   qty: 10,
   selling_price: 135.0,
   transaction_details_id: 1548837981076,
   transactions: #Ecto.Association.NotLoaded<association :transactions is not loaded>,
   updated_at: #DateTime<2019-01-30 08:46:40Z>,
   updated_by: "shan"

Please guide me how to access the values from this?

Do you know what is the data type returned? What have you tried so far? Which errors did you get?

The result type is a List I extracted like this

[head | _tail] = res

Here res is the List which comes as a result from Ecto query.

Query looks like this,

from(t in Transaction,
      join: td in TransactionDetail,
      where: td.transaction_details_id == t.transactions_id and t.transactions_id == ^id,
      lock: "FOR UPDATE",
      select: {t, td}
    |> Repo.all()
    |> case do
      [] ->
        Repo.rollback("Invalid checkout.")

      res ->
        {:ok, res}

The value that i put in the post is the value which comes from the head

I tried accessing like head.customer_id, head.transactions.customer_id.

Great! In your select, you are returning a tuple:

select: {t, td}

This means you first need to extract entries from the tuple before you access the fields. So for example, after you get the head:

{t, td} = head

What do you think?


Yeah that’s right. I missed that totally. I printed the head in console and based on that I tried things.

Thank you!

1 Like