实训点赞问题

dev_course
daiao 6 years ago
parent 7d81be96a5
commit 155e9a0a37

@ -10,13 +10,13 @@ class DiscussesController < ApplicationController
if current_user.admin? if current_user.admin?
@disscuss_count = Discuss.where(:dis_id => @container.id, :dis_type => @container.class.to_s, :root_id => nil).count @disscuss_count = Discuss.where(:dis_id => @container.id, :dis_type => @container.class.to_s, :root_id => nil).count
@discusses = Discuss.limit(LIMIT).where(:dis_id => @container.id, :dis_type => @container.class.to_s, @discusses = Discuss.limit(LIMIT).where(:dis_id => @container.id, :dis_type => @container.class.to_s,
:root_id => nil).includes(:user, :praise_tread).offset(offset) :root_id => nil).includes(:user, :praise_treads).offset(offset)
else else
disscusses = Discuss.where("dis_id = :dis_id and dis_type = :dis_type and root_id is null and disscusses = Discuss.where("dis_id = :dis_id and dis_type = :dis_type and root_id is null and
(hidden = :hidden or user_id = :user_id)", (hidden = :hidden or user_id = :user_id)",
{dis_id: @container.id, dis_type: @container.class.to_s, hidden: false, user_id: current_user.id}) {dis_id: @container.id, dis_type: @container.class.to_s, hidden: false, user_id: current_user.id})
@disscuss_count = disscusses.count @disscuss_count = disscusses.count
@discusses = disscusses.limit(LIMIT).includes(:user, :praise_tread).offset(offset) @discusses = disscusses.limit(LIMIT).includes(:user, :praise_treads).offset(offset)
end end
@current_user = current_user @current_user = current_user

@ -10,7 +10,7 @@ class Challenge < ApplicationRecord
has_many :games, :dependent => :destroy has_many :games, :dependent => :destroy
has_many :challenge_chooses, :dependent => :destroy has_many :challenge_chooses, :dependent => :destroy
has_many :homework_challenge_settings, :dependent => :destroy has_many :homework_challenge_settings, :dependent => :destroy
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy has_many :praise_treads, as: :praise_tread_object, dependent: :destroy
has_one :praise_tread_cache, as: :object, dependent: :destroy has_one :praise_tread_cache, as: :object, dependent: :destroy
has_many :tidings has_many :tidings
# 参考答案 # 参考答案

@ -5,7 +5,7 @@ class Discuss < ApplicationRecord
belongs_to :parent, class_name: 'Discuss', foreign_key: :parent_id, optional: true belongs_to :parent, class_name: 'Discuss', foreign_key: :parent_id, optional: true
has_many :children, -> { reorder(created_at: :asc) }, class_name: 'Discuss', foreign_key: :parent_id has_many :children, -> { reorder(created_at: :asc) }, class_name: 'Discuss', foreign_key: :parent_id
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy has_many :praise_treads, as: :praise_tread_object, dependent: :destroy
has_many :tidings, as: :container, dependent: :destroy has_many :tidings, as: :container, dependent: :destroy
has_one :praise_tread_cache, as: :object, dependent: :destroy has_one :praise_tread_cache, as: :object, dependent: :destroy

@ -47,7 +47,7 @@ class ShixunsService
if current_user.admin? if current_user.admin?
disscuss_count = Discuss.where(dis_id: dis_id, dis_type: dis_type, root_id: nil).count disscuss_count = Discuss.where(dis_id: dis_id, dis_type: dis_type, root_id: nil).count
discusses = Discuss.where(dis_id: dis_id, dis_type: dis_type, root_id: nil) discusses = Discuss.where(dis_id: dis_id, dis_type: dis_type, root_id: nil)
.includes(:user, :praise_tread).limit(LIMIT).offset(offset) .includes(:user, :praise_treads).limit(LIMIT).offset(offset)
else else
disscusses = Discuss.where( disscusses = Discuss.where(
'dis_id = :dis_id and dis_type = :dis_type and root_id is null and (hidden = :hidden or user_id = :user_id)', 'dis_id = :dis_id and dis_type = :dis_type and root_id is null and (hidden = :hidden or user_id = :user_id)',
@ -55,7 +55,7 @@ class ShixunsService
) )
disscuss_count = disscusses.count disscuss_count = disscusses.count
discusses = disscusses.includes(:user, :praise_tread).limit(LIMIT).offset(offset) discusses = disscusses.includes(:user, :praise_treads).limit(LIMIT).offset(offset)
end end
base_data discusses, dis, current_user base_data discusses, dis, current_user
@ -81,7 +81,7 @@ class ShixunsService
offset = page * LIMIT offset = page * LIMIT
find_status = true if position find_status = true if position
end end
discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).includes(:user, :praise_tread).offset(offset) discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).includes(:user, :praise_treads).offset(offset)
base_data discusses, dis, current_user base_data discusses, dis, current_user
Myshixun.find(params[:myshixun_id]).update_attribute(:onclick_time, Time.now) Myshixun.find(params[:myshixun_id]).update_attribute(:onclick_time, Time.now)
@ -109,8 +109,8 @@ class ShixunsService
if discusses.present? if discusses.present?
discusses.each do |d| discusses.each do |d|
# 总点赞数 # 总点赞数
praise_count = d.praise_tread.where(:praise_or_tread => 1).count praise_count = d.praises_count
user_praise= d.praise_tread.select{|pt| pt.user_id == current_user.id}.length > 0 ? true : false user_praise= d.praise_treads.select{|pt| pt.user_id == current_user.id}.length > 0 ? true : false
manager = current_user.manager_of_shixun?(dis, current_user) manager = current_user.manager_of_shixun?(dis, current_user)
game_url = game_url =
if manager if manager

@ -14,8 +14,8 @@ json.game_url discuss.game_url(container, current_user)
if discuss.parent_id if discuss.parent_id
json.can_delete discuss.can_deleted?(current_user) json.can_delete discuss.can_deleted?(current_user)
else else
json.praise_count discuss.praise_tread.where(praise_or_tread: 1).count json.praise_count discuss.praises_count
json.user_praise discuss.praise_tread.select{|pt| pt.user_id == current_user.id}.length > 0 ? true : false json.user_praise discuss.praise_treads.select{|pt| pt.user_id == current_user.id}.length > 0
end end

@ -2,7 +2,7 @@ class ModifyPraisesCountForChallenges < ActiveRecord::Migration[5.2]
def change def change
challenges = Challenge.where(nil).unscoped challenges = Challenge.where(nil).unscoped
challenges.find_each do |c| challenges.find_each do |c|
praises_count = c.praise_tread.where(praise_or_tread: 1).count praises_count = c.praise_treads.where(praise_or_tread: 1).count
c.update_column(:praises_count, praises_count) c.update_column(:praises_count, praises_count)
end end
end end

@ -0,0 +1,5 @@
class AddPraisesCountForDiscuess < ActiveRecord::Migration[5.2]
def change
add_column :discusses, :praises_count, :integer, :default => 0
end
end

@ -0,0 +1,9 @@
class ModifyPraisesCountForDiscuess < ActiveRecord::Migration[5.2]
def change
discusses = Discuss.includes(:praise_treads).unscoped
discusses.find_each do |d|
praises_count = d.praise_treads.liker.count
d.update_column(:praises_count, praises_count)
end
end
end
Loading…
Cancel
Save