Fixes NoMethodError: undefined method 'first' for nil:NilClass (#3036)

This commit is contained in:
alpaca-tc 2017-05-13 22:56:12 +09:00 committed by Eugen Rochko
parent 1027556614
commit e9810cbad6
2 changed files with 14 additions and 1 deletions

View File

@ -124,7 +124,7 @@ class Status < ApplicationRecord
def ancestors(account = nil) def ancestors(account = nil)
ids = Rails.cache.fetch("ancestors:#{id}") { (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id) } ids = Rails.cache.fetch("ancestors:#{id}") { (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id) }
statuses = Status.where(id: ids).group_by(&:id) statuses = Status.where(id: ids).group_by(&:id)
results = ids.map { |id| statuses[id].first } results = ids.map { |id| statuses[id]&.first }.compact
results = results.reject { |status| filter_from_context?(status, account) } results = results.reject { |status| filter_from_context?(status, account) }
results results

View File

@ -179,6 +179,19 @@ RSpec.describe Status, type: :model do
end end
end end
describe '#ancestors' do
it 'ignores deleted records' do
first_status = Fabricate(:status, account: bob)
second_status = Fabricate(:status, thread: first_status, account: alice)
# Create cache and delete cached record
second_status.ancestors
first_status.destroy
expect(second_status.ancestors).to eq([])
end
end
describe '#filter_from_context?' do describe '#filter_from_context?' do
pending pending
end end