Skip to content

Commit

Permalink
Fix preload_first_topic_post on empty association
Browse files Browse the repository at this point in the history
Reported in f1874fb#r33825204
  • Loading branch information
glebm committed Jun 6, 2019
1 parent 69e1cea commit c691be6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/concerns/thredded/post_common.rb
Expand Up @@ -22,6 +22,7 @@ module PostCommon
posts_table_name = quoted_table_name
result = all
owners_by_id = result.each_with_object({}) { |r, h| h[r.postable_id] = r.postable }
next result if owners_by_id.empty?
preloader = ActiveRecord::Associations::Preloader.new.preload(
owners_by_id.values, :first_post,
unscoped.where(<<~SQL.delete("\n"))
Expand Down
1 change: 1 addition & 0 deletions app/models/thredded/post_moderation_record.rb
Expand Up @@ -31,6 +31,7 @@ class PostModerationRecord < ActiveRecord::Base
posts_table_name = Thredded::Post.quoted_table_name
result = all
owners_by_id = result.each_with_object({}) { |r, h| h[r.post.postable_id] = r.post.postable }
next result if owners_by_id.empty?
preloader = ActiveRecord::Associations::Preloader.new.preload(
owners_by_id.values, :first_post,
Thredded::Post.unscoped.where(<<~SQL.delete("\n"))
Expand Down
9 changes: 9 additions & 0 deletions spec/models/thredded/post_moderation_record_spec.rb
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Thredded::PostModerationRecord do
it 'empty preload_first_topic_post' do
expect { described_class.preload_first_topic_post.to_a }.not_to raise_error
end
end

0 comments on commit c691be6

Please sign in to comment.