mirror of
https://github.com/discourse/discourse.git
synced 2024-12-14 13:56:20 +08:00
9b0300a647
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?).
37 lines
836 B
Ruby
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)
|
|
#
|