Blog: Comments
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue Book Search: BooksBookSearch: SeedingBlog: Comments
You’re going to create a comments table to your existing Blog project. You do not yet need to create the appropriate UI for creating a comment.
Comment/Post Association
A comment should belong to a blog post and a blog post should have_many comments.
Comments should have :content, and :post_id (foreign key) fields.
classDiagram
class Comment {
post_id: :id
content: :text
}
Hint
You will need to:
- Create the comments resource and run migrations.
$ mix phx.gen.html Comments Comment comments content:text post_id:references:posts
$ mix ecto.migrate
-
Define the has_many/3 relationship in the
Postschema. -
Define the belongs_to/3 relationship in the
Commentschema.
Once complete use the IEx shell to do the following:
- Create a new blog post.
- Use build_assoc/3 to create a comment associated with your created blog post.
- Use put_assoc/4 to create a new blog post and create multiple comments associated with the blog post.
- Use put_assoc/4 to add a comment to one of your existing blog posts.
- Use cast_assoc/3 to create a new blog post and create multiple comments associated with the blog post.
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum folder to commit your progress.
$ git add .
$ git commit -m "finish Blog: Comments exercise"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.