-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
5a88803
commit c20c9a9
Showing
7 changed files
with
54 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Comment < ApplicationRecord | ||
belongs_to :article | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |