Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Created October 17, 2019 11:55
Show Gist options
  • Save rebelweb/9b21117175d723e5491360b31af46a9f to your computer and use it in GitHub Desktop.
Save rebelweb/9b21117175d723e5491360b31af46a9f to your computer and use it in GitHub Desktop.
class Category < ApplicationRecord
has_many :article_categories, class_name: ArticleCategory, foreign_key: :category_id, dependent: :destroy
has_many :articles, through: :article_categories
end
class Article < ApplicationRecord
belongs_to :author, class_name: User, foreign_key: :author_id
has_many :article_categories, class_name: ArticleCategory, foreign_key: :article_id, dependent: :destroy
has_many :categories, through: :article_categories
has_many :images, class_name: ArticleImage, foreign_key: :article_id, dependent: :destroy
end
class ArticleCategory < ApplicationRecord
belongs_to :article, class_name: Article, foreign_key: :article_id
belongs_to :category, class_name: Category, foreign_key: :category_id, counter_cache: :articles_count
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment