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 => <name>''. Is it one of :artist? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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:11 pm, Christian Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 => > <name>''. Is it one of :artist?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ok, I got it to work with: @events = @artist.medias.where(:type => ''Event'') On Oct 12, 7:53 pm, Erol Fornoles <erol.forno...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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:11 pm, Christian Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > 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 => > > <name>''. Is it one of :artist? > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.