Not doing so has a few gotchas when working with relations, particularly with records which are not stored in the database. I'm excluding the related content file because it's got a very peculiar relationship with itself: the `has_one :opposite_related_content` has no inverse; the relation itself is its inverse. It's a false positive since the inverse condition is true: ``` content.opposite_related_content.opposite_related_content.object_id == content.object_id ```
24 lines
782 B
Ruby
24 lines
782 B
Ruby
class DirectMessage < ApplicationRecord
|
|
belongs_to :sender, class_name: "User", inverse_of: :direct_messages_sent
|
|
belongs_to :receiver, class_name: "User", inverse_of: :direct_messages_received
|
|
|
|
validates :title, presence: true
|
|
validates :body, presence: true
|
|
validates :sender, presence: true
|
|
validates :receiver, presence: true
|
|
validate :max_per_day
|
|
|
|
scope :today, lambda { where(created_at: Date.current.beginning_of_day..Date.current.end_of_day) }
|
|
|
|
def max_per_day
|
|
return if errors.any?
|
|
|
|
max = Setting[:direct_message_max_per_day]
|
|
return unless max
|
|
|
|
if sender.direct_messages_sent.today.count >= max.to_i
|
|
errors.add(:title, I18n.t("activerecord.errors.models.direct_message.attributes.max_per_day.invalid"))
|
|
end
|
|
end
|
|
end
|