Skip to content

Instantly share code, notes, and snippets.

@krishicks
Created July 9, 2011 12:52
Show Gist options
  • Save krishicks/1073563 to your computer and use it in GitHub Desktop.
Save krishicks/1073563 to your computer and use it in GitHub Desktop.
Double-polymorphic has_many through modeling in Rails
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
@krishicks
Copy link
Author

Sorry to say, I have no idea. What you said sounds plausible, though. Note, this gist is 10 years old!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment