2025-08-05 16:11:24 +02:00
|
|
|
class Mood < ApplicationRecord
|
2026-01-28 16:34:06 +01:00
|
|
|
belongs_to :user
|
2026-03-14 16:35:48 +01:00
|
|
|
belongs_to :mode
|
2026-03-20 18:13:12 +01:00
|
|
|
|
|
|
|
|
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
|
2025-08-05 16:11:24 +02:00
|
|
|
end
|