On Wednesday, 7 October 2015 20:47:54 UTC-4, Ruby-Forum.com User wrote: > > 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 > nil. > > class Artist < ActiveRecord::Base > has_many :artist_events, dependent: :destroy > has_many :events, through: :artist_events > end > > class Event < ActiveRecord::Base > belongs_to :artist_event
has_one :artist, through: :artist_events > Something hasn't been copied correctly here, because this shouldn't work - the `through` refers to an association (artist_events) that doesn't exist on Event. As noted elsewhere, `belongs_to` is likely not the right association to use here; it's expecting an 'artist_event_id' column on the 'events' table. You'll also want to carefully consider if `has_one` is the right association as well. The table structure you've set up (Artist / ArtistEvent / Event) is a classic many-to-many relationship. If an Event can truly only have one artist, this structure is not needed. If an Event can have many artists, than a has_one isn't correct. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2fa1934c-fe96-42e6-bcb2-fa4575d30f8a%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

