diff --git a/app/models/topic.rb b/app/models/topic.rb index d9821f83b04..327f12b5972 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -76,6 +76,7 @@ class Topic < ActiveRecord::Base belongs_to :category has_many :posts + has_many :ordered_posts, -> { order(post_number: :asc) }, class_name: "Post" has_many :topic_allowed_users has_many :topic_allowed_groups diff --git a/lib/flag_query.rb b/lib/flag_query.rb index 7178bba2c90..76809c652ae 100644 --- a/lib/flag_query.rb +++ b/lib/flag_query.rb @@ -42,7 +42,7 @@ module FlagQuery end post_actions = actions.order('post_actions.created_at DESC') - .includes(related_post: { topic: { posts: :user }}) + .includes(related_post: { topic: { ordered_posts: :user }}) .where(post_id: post_ids) post_actions.each do |pa| @@ -67,13 +67,13 @@ module FlagQuery if pa.related_post && pa.related_post.topic conversation = {} related_topic = pa.related_post.topic - if response = related_topic.posts[0] + if response = related_topic.ordered_posts[0] conversation[:response] = { excerpt: excerpt(response.cooked), user_id: response.user_id } user_ids << response.user_id - if reply = related_topic.posts[1] + if reply = related_topic.ordered_posts[1] conversation[:reply] = { excerpt: excerpt(reply.cooked), user_id: reply.user_id