Please help me in finding the elixir/ecto option for :source option of has_one/has_many :through association of ActiveRecord.
Here is my problem, I have the following schemas: User, Event, AttendingEvents. A User has many events, and an Event belongs to a user. Then there is AttendedEvents which belongs to both user and group. So, in rails I would do this:
class User < ApplicationRecord
has_many :attending_events, dependent: :destroy
has_many :attended_events, through: :attending_events, source: :event, foreign_key: :attendee_id
end
class Event < ApplicationRecord
has_many :attending_events
has_many :attendees, through: :attending_events, source: :user
end
Since ecto has no source, how can I rewrite this so that I am able to do this instead of a query?
event = MyApp.Repo.get(MyApp.Event, id) |> MyApp.Repo.preload(:attendees)
event.attendees
If I understand your schema and requests you can do like this.
With these schemas and mapping your query became:
Does it make sense?