6.2 Associating Models

Active Record associations let you easily declare the relationship
between two models. In the case of comments and posts, you could write
out the relationships this way:

    Each comment belongs to one post.
    One post can have many comments.

In fact, this is very close to the syntax that Rails uses to declare
this association. You've already seen the line of code inside the
Comment model (app/models/comment.rb) that makes each comment belong to
a Post:
class Comment < ActiveRecord::Base
  belongs_to :post
end

You'll need to edit app/models/post.rb to add the other side of the
association:
class Post < ActiveRecord::Base
  has_many :comments
  validates :title, presence: true,
                    length: { minimum: 5 }
  [...]
end

These two declarations enable a good bit of automatic behavior. For
example, if you have an instance variable @post containing a post, you
can retrieve all the comments belonging to that post as an array using
@post.comments.