Skip to content

Commit

Permalink
Add Comment model and nested route
Browse files Browse the repository at this point in the history
Add Comment model and migration with a foreign key reference to the
Article model. Make the comment a nested resource under the Article
resource so that each comment resource requires an :article_id parameter
in the URL.

Add has_many association in the Article model and include the dependent:
:destroy option so that the Article destroy tests don't fail.

Completes Rails Getting Started Guide:
* 8 Adding a Second Model
  * 8.1 Generating a Model
  * 8.2 Associating Models
  * 8.3 Adding a Route for Comments
  • Loading branch information
msducheminjr committed Dec 29, 2021
1 parent 5a88803 commit c20c9a9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Article < ApplicationRecord
has_many :comments, dependent: :destroy

validates :title, presence: true
validates :body, presence: true, length: { minimum: 10 }
end
3 changes: 3 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Comment < ApplicationRecord
belongs_to :article
end
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Rails.application.routes.draw do
root "articles#index"

resources :articles
resources :articles do
resources :comments
end

resources :posts
end
11 changes: 11 additions & 0 deletions db/migrate/20211229220150_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateComments < ActiveRecord::Migration[7.0]
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :article, null: false, foreign_key: true

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

nerd_gm:
commenter: Nerdy Game Master
body: I love how i can do something like NerdDice.roll_4d6_with_advantage_3 and it just works!
article: nerd

nerd_ruby:
commenter: Ruby Enthusiast
body: Thanks for the episode on method_missing and Ruby metaprogramming! It really helped me out.
article: nerd

why_visionary:
commenter: Visionary
body: That Start With Why video is my favorite TED talk!
article: why
7 changes: 7 additions & 0 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class CommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit c20c9a9

Please sign in to comment.