Challenge 2: has_many :through for Tags — Solution # app/models/post.rb class Post < ApplicationRecord has_many :taggings has_many :tags, through: :taggings end # app/models/tag.rb class Tag < ApplicationRecord has_many :taggings has_many :posts, through: :taggings end # app/models/tagging.rb class Tagging < ApplicationRecord belongs_to :post belongs_to :tag end # Getting all tags for a given post: @post.tags =begin Notes: - Tagging is the join model -- it's the only one of the three with two belongs_to declarations, one for each side of the many-to-many relationship it connects. - Post and Tag are structurally symmetric: each has_many :taggings (its direct, one-to-many relationship to the join table) plus has_many :X, through: :taggings (the actual many-to-many relationship reached by going through that join table). - @post.tags silently performs a posts -> taggings -> tags join behind one method call, the exact three-table SQL JOIN the chapter contrasted this against having to write by hand. - A Tagging table would need its own migration (following the Chapter 5 pattern) with post_id and tag_id foreign key columns for any of this to actually work against a real database. =end