Dynamic Like & Dislike Buttons in Your Rails 6 Application

How to implement a like button the simple way

Maria Schuessler
The Startup

--

Photo by Kelly Sikkema on Unsplash

When I first started with Ruby on Rails, one of the exercises that we had to do was to recreate the structure of Twitter — a user could post short ‘messages’, follow and be followed by other users.

We did this for two reasons: the first to show that the technology behind Twitter — at the time built using Ruby on Rails — was quite simple if you looked under the hood. The second was to understand how polymorphic (many-to-many) relationships worked in a relational database.

A like inside of an application is really a save for later. It is a way to access a smaller subset of data in a sea of records. In relational databases using SQL, likes are typically stored on their own table. However, creating that relationship is kind of a pain. Luckily, with Ruby, there’s always a gem to make things simpler. For this demo, I’ll use acts-as-favoritor.

Let’s get started.

Step 1: Set up an application with two models

To get starting with liking, you need just two things:

  • A model that performs the liking action (the user)
  • A model that is liked (any object like a book, a restaurant…

--

--

Maria Schuessler
The Startup

Music Product @ TikTok | Former Full-Stack Dev | Editor of StirCrazy! Mag | London-based | skippingcustoms.com