Drummer Cymbal , STI . type, , has_many, :
class Subject < ActiveRecord::Base
has_many :subject_videos, :dependent => :destroy
has_many :videos, :through => :subject_videos
end
class Drummer < Subject
end
class Cymbal < Subject
end
SubjectVideo subject_id video_id. :
class Video < ActiveRecord::Base
has_many :subject_videos, :dependent => :destroy
has_many :subjects, :through => :subject_videos
end
"--".
d = Drummer.create
d.videos
d.videos.create(:name=>"Stompin' at the Savoy")
v = Video.find_by_name("Stompin' at the Savoy")
v.subjects
The main drawback of this approach is that Drummer and Cymbal are now stored in the same table, which may be undesirable if they have multiple columns.
If you still need to use many-to-many relationships using polymorphism, see also has_many_polymorphs .
source
share