Sanjibukai

Sanjibukai

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..

Showing Posts 1 to 8

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.

Sanjibukai

Sanjibukai OP

Thanks for the response…
However, I was pretty sure my explanations will be confusing…

I have a single table in the database with many columns…

But I have different Schemas (for different context) that lists only part of what they deal with…

So it’s not a join per se, right?

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 OP

Thanks.. I’ll try that!

pmangalakader

pmangalakader

You can directly query using the table name (Ecto.Query — Ecto v3.14.0) instead of using the contexts.

q = from c in "cars" #tablename here
      where: c.id == c.sales_car_id #comparison columns
      select: {c.price, c.mileage}

The joins clause probably is an overkill and it’s way better to do it this way. I misinterpreted the real question probably like two tables two contexts and hence the dilemma.

Sanjibukai

Sanjibukai OP

Well.. I wonder why I didn’t think for that..
In fact my models are a little more complex..
And the best would have been to be able to have “free access” to any fields, like when we are doing a Repo.preload..

E.g. it’s kinda like inheritance.. A Sales.Car is a Mech.Car.. But yeah… I don’t like to go into STI and the like..

Honestly I think I’ll simply fetch two times the DB and give back each models in a tuple..

pmangalakader

pmangalakader

Welcome to the club!!! :hugs: :hugs: :hugs:

al2o3cr

al2o3cr

Consider making this use case a third context with its own schemas.

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

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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews