Scoping Validations in ActiveRecord

// Apr 22, 2015

In ActiveRecord, you can add something called :scope to your validations, which simply means validate an object WITHIN the scope of another object.

So if you had a Upvote class which needed to validate if a user has upvoted on a post already, the proper validation would be:

class Upvote < ActiveRecord::Base
  belongs_to :post
  belongs_to :user

  validates :post_id, uniqueness: { true, scope: :user_id }


This will validate the uniqueness of the post_id WITHIN the scope of the user_id, as to make sure that there is never a duplicate post_id for a single user_id.

This will properly ensure that a post cannot be liked by a user more than once.