today_ai_weather/app/models/vote.rb
songtianlun e5c1461e8a feat: add voting functionality for votable entities
- Create VotesController to manage voting actions
- Implement Vote model with polymorphic associations
- Add vote buttons to the city and weather art views
- Integrate vote counting and user vote tracking
- Define routes for the votes resource

This commit sets up a voting mechanism for cities and weather arts, allowing users to upvote or downvote. It includes seamless integration of user sessions to track individual votes, ensuring votes can be modified or deleted based on user interaction.
2025-02-07 13:10:53 +08:00

13 lines
341 B
Ruby

class Vote < ApplicationRecord
belongs_to :votable, polymorphic: true
enum :vote_type, { upvote: 0, downvote: 1 }
validates :user_session, presence: true
validates :votable_type, presence: true
validates :votable_id, presence: true
validates :vote_type, presence: true
scope :for_object, ->(obj) { where(votable: obj) }
end