|
# STI parent |
|
# |
|
# id :integer not null, primary key |
|
# type :string(255) not null |
|
# title :string(255) not null |
|
# content :text |
|
# |
|
class Post < ActiveRecord::Base |
|
scope :articles, -> { where(type: "Article") } |
|
scope :pages, -> { where(type: "Page") } |
|
end |
|
|
|
# STI child (taggable) |
|
# |
|
# Uses posts table. |
|
# |
|
class Article < Post |
|
has_many :taggings, as: :taggable, dependent: :destroy |
|
has_many :tags, through: :taggings |
|
end |
|
|
|
# STI child (taggable) |
|
# |
|
# Uses posts table. |
|
# |
|
class Page < Post |
|
has_many :taggings, as: :taggable, dependent: :destroy |
|
has_many :tags, through: :taggings |
|
end |
|
|
|
# Independent model (taggable) |
|
# |
|
# id :integer not null, primary key |
|
# title :string(255) not null |
|
# url :string(255) not null |
|
# |
|
class Video < ActiveRecord::Base |
|
has_many :taggings, as: :taggable, dependent: :destroy |
|
has_many :tags, through: :taggings |
|
end |
|
|
|
# Many-to-many polymorphic table |
|
# |
|
# id :integer not null, primary key |
|
# tag_id :integer not null |
|
# taggable_id :integer not null |
|
# taggable_type :string(255) not null |
|
# |
|
class Tagging < ActiveRecord::Base |
|
belongs_to :tag |
|
belongs_to :taggable, polymorphic: true |
|
end |
|
|
|
# Attach a tag to any model through taggings |
|
# |
|
# id :integer not null, primary key |
|
# title :string(255) not null |
|
# |
|
class Tag < ActiveRecord::Base |
|
has_many :taggings, dependent: :destroy |
|
|
|
with_options through: :taggings, source: :taggable do |tag| |
|
tag.has_many :articles, source_type: "Post", class_name: "Article" |
|
tag.has_many :pages, source_type: "Post", class_name: "Page" |
|
tag.has_many :videos, source_type: "Video" |
|
end |
|
end |