Implement a "Likes system" in rails

Viewed 66

I'm trying to figure out how to setup the following. A user can create a review and then like his review or like other reviews if he wants to. I came up with the following setup:

class Like < ApplicationRecord
  belongs_to :user
  belongs_to :review
end

class User < ApplicationRecord
  has_many :reviews
  has_many :likes
  has_many :liked_reviews, through: :likes, source: :review
end

class Review < ApplicationRecord
  belongs_to :user
  has_many :likes
  has_many :liking_users, :through => :likes, :source => :user
end 

class CreateLikes < ActiveRecord::Migration[6.0]
  def change
    create_table :likes do |t|
      t.references :user, index: true
      t.references :likes, index: true
      t.integer :review_id

      t.timestamps
    end
  end
end

I'm pretty sure that the model associations I came up with are correct, but I'm not so sure about the like table. Can someone please review the above and tell me if the model associations and like table are correct? Thanks in advance!

0 Answers
Related