Created
July 9, 2011 12:52
-
-
Save krishicks/1073563 to your computer and use it in GitHub Desktop.
Double-polymorphic has_many through modeling in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Article < ActiveRecord::Base | |
has_many :content_relationships, as: :origin, dependent: :destroy | |
has_many :related_articles, through: :content_relationships, source: :related_content, source_type: "Article" | |
has_many :related_videos, through: :content_relationships, source: :related_content, source_type: "Video" | |
end | |
class Video < ActiveRecord::Base | |
has_many :content_relationships, as: :origin, dependent: :destroy | |
has_many :related_articles, through: :content_relationships, source: :related_content, source_type: "Article" | |
has_many :related_videos, through: :content_relationships, source: :related_content, source_type: "Video" | |
end | |
class ContentRelationship < ActiveRecord::Base | |
belongs_to :origin, polymorphic: true | |
belongs_to :related_content, polymorphic: true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry to say, I have no idea. What you said sounds plausible, though. Note, this gist is 10 years old!