|
|
@ -4,6 +4,8 @@ class Message < ApplicationRecord
|
|
|
|
belongs_to :board, counter_cache: true
|
|
|
|
belongs_to :board, counter_cache: true
|
|
|
|
belongs_to :author, class_name: "User", foreign_key: 'author_id'
|
|
|
|
belongs_to :author, class_name: "User", foreign_key: 'author_id'
|
|
|
|
belongs_to :parent, class_name: "Message", foreign_key: "parent_id", counter_cache: :replies_count, optional: true
|
|
|
|
belongs_to :parent, class_name: "Message", foreign_key: "parent_id", counter_cache: :replies_count, optional: true
|
|
|
|
|
|
|
|
belongs_to :root, class_name: 'Message', foreign_key: :root_id, counter_cache: :descendants_count, optional: true
|
|
|
|
|
|
|
|
|
|
|
|
has_one :message_detail, dependent: :destroy
|
|
|
|
has_one :message_detail, dependent: :destroy
|
|
|
|
accepts_nested_attributes_for :message_detail, update_only: true
|
|
|
|
accepts_nested_attributes_for :message_detail, update_only: true
|
|
|
|
|
|
|
|
|
|
|
@ -12,6 +14,7 @@ class Message < ApplicationRecord
|
|
|
|
has_many :tidings, as: :container, dependent: :destroy
|
|
|
|
has_many :tidings, as: :container, dependent: :destroy
|
|
|
|
has_many :attachments, as: :container, dependent: :destroy
|
|
|
|
has_many :attachments, as: :container, dependent: :destroy
|
|
|
|
has_many :course_acts, :class_name => 'CourseActivity',:as =>:course_act ,:dependent => :destroy # 课程动态
|
|
|
|
has_many :course_acts, :class_name => 'CourseActivity',:as =>:course_act ,:dependent => :destroy # 课程动态
|
|
|
|
|
|
|
|
has_many :descendants, class_name: 'Message', foreign_key: :root_id, dependent: :destroy
|
|
|
|
|
|
|
|
|
|
|
|
scope :root_nodes, -> { where("parent_id IS NULL") } #判断该信息是帖子还是回复。null为发布的帖子
|
|
|
|
scope :root_nodes, -> { where("parent_id IS NULL") } #判断该信息是帖子还是回复。null为发布的帖子
|
|
|
|
scope :reply_nodes, -> { where("parent_id IS NOT NULL") }
|
|
|
|
scope :reply_nodes, -> { where("parent_id IS NOT NULL") }
|
|
|
@ -70,24 +73,16 @@ class Message < ApplicationRecord
|
|
|
|
|
|
|
|
|
|
|
|
# 包含二级回复的总点赞数
|
|
|
|
# 包含二级回复的总点赞数
|
|
|
|
def total_praises_count
|
|
|
|
def total_praises_count
|
|
|
|
praises_count + children.includes(:children).reduce(0) { |count, filed|
|
|
|
|
descendants.sum(:praises_count)
|
|
|
|
sub_sum_count = filed.children.reduce(0) { |sub_count, sub_filed| sub_count += sub_filed.praises_count }
|
|
|
|
|
|
|
|
count += filed.praises_count
|
|
|
|
|
|
|
|
sub_sum_count += count
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# 包含二级回复数的总回复数
|
|
|
|
# 包含二级回复数的总回复数
|
|
|
|
def total_replies_count
|
|
|
|
def total_replies_count
|
|
|
|
replies_count + children.includes(:children).reduce(0) { |count, child|
|
|
|
|
descendants_count
|
|
|
|
sub_sum_count = child.children.reduce(0) { |sub_count, sub_child| sub_count += sub_child.replies_count }
|
|
|
|
|
|
|
|
count += child.replies_count
|
|
|
|
|
|
|
|
sub_sum_count += count
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def has_replies
|
|
|
|
def has_replies
|
|
|
|
children.present?
|
|
|
|
children.exists?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|