December 18, 2014

Project: Clone Reddit using Rails

Polymorphism and Duck-typing

Since comments can be related to Posts and other Comments, we’ll begin by reviewing Rail’s implementation of polymorphic relationships and duck typing.

Task: review polymorphism in Rails:

Task: review duck-typing:

Q: Compare and contrast duck-typing and class inheritance.

Task: Make comments invalid without a polymorphic owner.

Validations and Database Constraints

While Rails validations make it easy to ensure that a Comment is not valid without a parent, there are other common ways to enforce these validations much more performantly. For example, database-level constraints are common in financial or big-data applications where the application is the bottleneck of many computations.

Task: Review the PostgreSQL implementation of database constrations:

Q: What are some reasons that ActiveRecord validations are preferred over database constraints in Rails?

Q: Considering your answer to the previous question, what are some types of data that would be good to validate in a Rails application using database constraints?

Updating the Comments Controller

Task: Do not allow comments to be deleted, only to be nilled out by removing the author and content.

Q: Given the context of Reddit, why would we want to nullify posts instead of deleting them?

Task: when a comment is created, the user should be redirected to the owning object, not CommentsController#show.

Integrate partials for Comments

Capybara is a commonly used integrational testing library for Ruby. Using Capybara, we can write tests that simulate actual user interaction with our application, like logging in or clicking post links.

Task: Read a getting started guide for Capybara:

Task: Read the Capybara documentation:

Task: Implement the following acceptance tests using Capybara while implementing Comments:

  • User signs up and creates a post.

  • User logs in and comments on a post.

  • User creates a comment then deletes the comment.

  • User logs in and comments on a Comment.


If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository.

Join Make School Icon

Join Make School

Prepare for your career as a founder or software developer

Apply now
Talk to Us Icon

Talk to Us

If you have any questions, please send us an email.

Email Us