Creating associations, but not using a migration

Hello all,

I am working through the Getting Started with Rails tutorial at the official Rails Guides website.

In section 6.1 there is:

$ bin/rails generate model Comment commenter:string body:text article:references # The emphasis in this and later block quotes is my own.

I then run the migration, which automatically creates the first half of a one-to-many association:

# app/models/comment.rb:

class Comment < ApplicationRecord
  belongs_to :article
end

Then in section 6.2, I manually add the second half of the association, by adding ‘has_many :comments’ to the Article class definition:

# app/models/article.rb:

class Article < ApplicationRecord
  has_many :comments
  validates :title, presence: true, length: { minimum: 5 }
end

My question is this:
Why do I manually create the second half of the association, if I used a migration to create the first half?

My follow-up question is this:
Can I manually create the first half of the association manually, rather than using a migration?

This is my first post on the Ruby Forum. Therefore, special thanks in advance to those who respond. I understand there is no need for me to sign posts, so I will finish the post by saying…

Sincerely,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs