Active Record has_one through

I have two tables in my app which I am attempting to relate through a
join table. The Artist class which uses ‘has_many through’, works as
expected. However, the Event class using ‘has_one through’, brings back

class Artist < ActiveRecord::Base
has_many :artist_events, dependent: :destroy
has_many :events, through: :artist_events

class Event < ActiveRecord::Base
belongs_to :artist_event
has_one :artist, through: :artist_events

class ArtistEvent < ActiveRecord::Base
belongs_to :artist
belongs_to :event

In irb i get all events for an artist with “” (where a =
Artist.first). But e.artist returns ‘nil’. Whats strange is that
a.artist_event returns nill as well. However, when I run a ‘find_by’ on
the artist_event table and use, the artist_event is returned.

I have tried changing the Event class to use has_one as opposed to
belongs_to, without effect.

What am I missing? Do I need to have an index in the schema?

why dont you just do this…

class Artist < ActiveRecord::Base
has_many :events, dependent: :destroy

class Event < ActiveRecord::Base
belongs_to :artist

a = Artist.first which will return a list of events connected to that artist

e = Event.first
e.artist which will return the artist that is connected to that event.

Maybe explain what your user story is to better understand. With what
you posted I comprehended the statement above.

Turns out the event table needed to have an artist_id column. I was
attempting to use the join table without a reference to the artist

I dont know if that was the fix, because i also wrote a function in the
event model that used the artist_id to return artist data from the

I did try your suggestion when initially building the classes…but
again, without the artist_id in the event table, it failed.

class Event < ActiveRecord::Base
has_many :city_events, dependent: :destroy

validates :EventID, uniqueness: true

def artist

def city