diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 286935c17..6b686e0e7 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -53,7 +53,6 @@ class CoursesController < ApplicationController # c.tea_id=u.id and u.id=ue.user_id and ue.school_id=s.id") @courses = Course.where(is_delete: 0, is_hidden: 0) - .includes(:course_modules, :course_members, teacher: [user_extension: :school]) .order("courses.id = 1309 desc, courses.created_at desc") # @courses = Course.where(is_delete: 0, is_hidden: 0).select("courses.id, courses.tea_id, courses.name, courses.exercises_count, courses.polls_count, @@ -66,13 +65,11 @@ class CoursesController < ApplicationController .order("courses.id = 1309 DESC, courses.#{order_str} DESC") elsif @order == "created_at" # REDO:Extension - @courses = Course.joins(teacher: :user_extension) - .where(is_delete: 0, is_hidden: 0, is_end: 0) + @courses = Course.where(is_delete: 0, is_hidden: 0, is_end: 0) .order("courses.id = 1309 DESC, courses.#{order_str} DESC") else # REDO:Extension - @courses = Course.joins(teacher: :user_extension) - .where(is_delete: 0, is_hidden: 0) + @courses = Course.where(is_delete: 0, is_hidden: 0) .order("courses.id = 1309 DESC, courses.#{order_str} DESC") end @@ -89,7 +86,7 @@ class CoursesController < ApplicationController (course_members.role in(1,2,3) and CONCAT(users.lastname, users.firstname) like :keyword) or courses.name like :keyword or schools.name like :keyword } - @courses = @courses.joins(course_members: [user: [user_extension: :school]]) + @courses = @courses.joins(:school, course_members: [user: :user_extension]) .where("#{sql}", keyword: "%#{params[:search]}%").distinct end @courses_count = @courses.size @@ -98,7 +95,8 @@ class CoursesController < ApplicationController page = params[:page] || 1 limit = params[:limit] || 16 - @courses= @courses.page(page).per(limit) + @courses = @courses.page(page).per(limit) + @courses = @courses.preload(:school, :course_members, teacher: :user_extension) end def visits_plus_one @@ -655,7 +653,8 @@ class CoursesController < ApplicationController page = params[:page] || 1 limit = params[:limit] || 20 - @students= Kaminari.paginate_array(@students).page(page).per(limit) + @students= @students.page(page).per(limit) + @students = @students.includes(:course_group, user: :user_extension) end # 获取当前课程所有分班 diff --git a/app/models/user.rb b/app/models/user.rb index 7e04ff423..523df045d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -318,7 +318,12 @@ class User < ApplicationRecord elsif business? Course::BUSINESS else - role = course.course_members.find_by(user_id: id, is_active: 1)&.role + role = + if course.course_members.loaded? + course.course_members.find{|member| member.user_id == id && member.is_active == 1}&.role + else + course.course_members.find_by(user_id: id, is_active: 1)&.role + end case role when nil then Course::NORMAL when 'CREATOR' then Course::CREATOR diff --git a/app/views/courses/index.json.jbuilder b/app/views/courses/index.json.jbuilder index 5a128a0b7..2b6866517 100644 --- a/app/views/courses/index.json.jbuilder +++ b/app/views/courses/index.json.jbuilder @@ -1,18 +1,15 @@ -json.courses do - json.array! @courses do |course| +json.courses @courses do |course| json.id course.id json.name course.name json.avatar_url url_to_avatar(course.teacher) json.creator course.teacher.real_name - json.school course.teacher.school_name - json.technical_title course.teacher.user_extension.technical_title + json.school course.school&.name + json.technical_title course.teacher.identity json.course_members_count course.course_members_count json.tasks_count get_tasks_count course json.visits course.visits json.is_public course.is_public json.is_accessible course.is_public == 1 || @user.course_identity(course) < Course::NORMAL json.is_end course.is_end - json.first_category_url module_url(course.course_modules.not_hidden.first, course) - end end json.courses_count @courses_count