# == 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) # class Vote < ApplicationRecord belongs_to :user belongs_to :votable, polymorphic: true, counter_cache: true enum :direction, {upvote: 0, downvote: 1} validates :user_id, uniqueness: { scope: [:votable_type, :votable_id], message: "has already voted on this item" } end