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: https://www.railstutorial.org/book/following_users

Task: review duck-typing: http://rubylearning.com/satishtalim/duck_typing.html

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: http://www.postgresql.org/docs/8.2/static/ddl-constraints.html

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: http://www.sitepoint.com/basics-capybara-improving-tests/

Task: Read the Capybara documentation: http://www.rubydoc.info/github/jnicklas/capybara

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 or comment below.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations in San Francisco and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more

Cookies on Make School's website

We have placed cookies on your device to ensure that we give you the best experience on our website.

This site uses cookies to deliver our services. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Your use of Make School’s Products and Services is subject to these policies and terms.

Please note that Make School no longer supports Internet Explorer

We recommend upgrading to a modern web browser. Learn more