The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

33 lines
732 B

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: poll_votes
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # poll_id :bigint(8)
  9. # choice :integer default(0), not null
  10. # created_at :datetime not null
  11. # updated_at :datetime not null
  12. # uri :string
  13. #
  14. class PollVote < ApplicationRecord
  15. belongs_to :account
  16. belongs_to :poll, inverse_of: :votes
  17. validates :choice, presence: true
  18. validates_with VoteValidator
  19. after_create_commit :increment_counter_cache
  20. delegate :local?, to: :account
  21. private
  22. def increment_counter_cache
  23. poll.cached_tallies[choice] = (poll.cached_tallies[choice] || 0) + 1
  24. poll.save
  25. end
  26. end