price-point-api/app/models/vote.rb

30 lines
787 B
Ruby
Raw Normal View History

# == Schema Information
#
# Table name: votes
#
# id :bigint not null, primary key
# direction :integer
# votable_type :string not null
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null
# votable_id :bigint not null
#
# Indexes
#
# index_votes_on_user_id (user_id)
# index_votes_on_votable (votable_type,votable_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
2026-05-30 16:15:24 +00:00
class Vote < ApplicationRecord
belongs_to :user
belongs_to :votable, polymorphic: true, counter_cache: true
2026-05-30 16:15:24 +00:00
enum :direction, {upvote: 0, downvote: 1}
validates :user_id, uniqueness: { scope: [:votable_type, :votable_id], message: "has already voted on this item" }
end