Sanjibukai

Sanjibukai

How to fetch data field between 2 models from 2 Context Schema (that share the same Table)

Hello everybody!

I love how Phoenix (with Ecto) allows clean boundaries thanks to context and schemas.
But I just came across something that seems trivial yet I’m not able to find how to manage that..

The question might be confusing so here is a quick example..
Let’s say I have one database table about cars..
And I have two contexts one for the sales and one for the mechanics.
So Sales.Car deal with sales related data like price etc. while Mech.Car deal with technical datas and metrics about the car..
Each Schema on each Context having their own changeset it’s simple and clean etc.

However, now I want to display somewhere like in a Dashboard, data from both context/schemas…
Let’s say for example Sales.Car.price and Mech.Car.mileage..

If I already have access to one Model, e.g Sales.Car in one of my controller (which I fetched from the database).. Now how can I have access to the data from the other context?

This seems really trivial, but yet I have no clue how to do that..

If it was two different tables, I have put a relation like CarMetrics belongs to Car (which CarMetrics would had the car_id), and I would have fetch the Car with the CarMetrics preloaded.

However, here there’s no key to reference..

One easy solution could be to fetch from the other context using the ID of my previous fetch..

sales_car = Sales.get_car(..)
metrics_car = Mech.get_car(sales_car.id)

But here, there will be two database query right..
So is it possible to have access to those columns with one query?

Thank you for any suggestions..

Most Liked

pmangalakader

pmangalakader

You can use the ecto joins clause combined with a where match clause.

Here’s an article, that details it out: Please feel free to skip ahead to InnerJoins section in the following:

Official link:

It’s easier to add a reference key in your relationship table to make this simpler, but I’m not aware of the intricate details of your implementation.

pmangalakader

pmangalakader

Even though they are different contexts, you can access using alias in the other context for using them in your join clause.

Your query should be like in your Sales Context file:

alias Mech.Car

q = from s in Sales.Car,
       joins: m in Mech.Car,
       where: s.id == m.sales_id,
       select: %{price: s.price, mileage: m.mileage}

Repo.all(q)
Sanjibukai

Sanjibukai

Thanks.. I’ll try that!

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

9mm
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement