class Mood < ApplicationRecord belongs_to :user belongs_to :mode validates :recorded_at, presence: true validate :unique_per_day_and_user private def unique_per_day_and_user return unless recorded_at && user existing = user.moods.where.not(id: id).any? do |mood| mood.recorded_at.to_date == recorded_at.to_date end errors.add(:recorded_at, :taken) if existing end end