I think this is a great example of where join schemas shine. Assuming you have a schema defined as such…
defmodule YourApp.UserOrganization do
use Ecto.Schema
schema "users_organizations" do
belongs_to :organization, YourApp.Organization
belongs_to :user, YourApp.User
timestamps()
end
....
end
You could run a query like so
organization_id = 1
YourApp.UserOrganization
|> where([uo], uo.organization_id == ^organization_id)
|> preload(:user)
|> Repo.all
Disclaimer: I prefer the pipeline syntax for Ecto queries, feel free to switch over to from if you like ![]()
Now, you have a list of UserOrganization structs which include the timestamps of when the user joined the organization. If you need to access the user itself, just use the assoc (which was preloaded above for demonstration).


















