discourse/plugins/poll/app/models/poll_option.rb
Bianca Nenciu 9b0300a647
PERF: Preload voters_count and has_voted (#28808)
These fields are often used when serializing topics which may contain
multiple polls. On average, serializing a poll took 2+N queries where N
is the number of options. This change reduces the number of queries to
3, one for each field (Poll#voters_count, PollOption#voters_count and
Poll#has_voted?).
2024-09-10 18:41:08 +03:00

37 lines
836 B
Ruby

# frozen_string_literal: true
class PollOption < ActiveRecord::Base
belongs_to :poll
has_many :poll_votes, dependent: :delete_all
attr_writer :voters_count
def voters_count
return @voters_count if defined?(@voters_count)
@voters_count = poll_votes.count
end
end
# == Schema Information
#
# Table name: poll_options
#
# id :bigint not null, primary key
# poll_id :bigint
# digest :string not null
# html :text not null
# anonymous_votes :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_poll_options_on_poll_id (poll_id)
# index_poll_options_on_poll_id_and_digest (poll_id,digest) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (poll_id => polls.id)
#