STI and :through, not working?

Artist.first.medias returns all medias. Media is an STI. Media can be
video or painting.

How do I return all paintings of an artist? Only paintings, not
videos?

In my Artist model, I tried putting:

has_many :paintings, :through => :medias

In console, I do:

Artist.first.paintings.all

And I get:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could
not find the source association(s) :painting or :paintings in model
Media. Try 'has_many :paintings, :through => :medias, :source =>
'. Is it one of :artist?

That’s not how :through works. has_many :through associations are only
used when there is an intermediate model between them.

What you can do is:

has_many :medias, :class_name => “Media” # Will return all media,
including paintings and videos
has_many :paintings, :class_name => “Painting”
has_many :videos, :class_name => “Video”

HTH

On Oct 12, 7:11pm, Christian F. [email protected]

Ok, I got it to work with:

@events = @artist.medias.where(:type => ‘Event’)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs