How do you validate uniqueness of a pair of ids in Ruby on Rails?

前端 未结 6 2064
梦如初夏
梦如初夏 2020-12-04 17:31

Suppose the following DB migration in Ruby:

    create_table :question_votes do |t|
      t.integer :user_id
      t.integer :question_id
      t.integer :vote

          


        
6条回答
  •  囚心锁ツ
    2020-12-04 18:00

    From RailsGuides. validates works too:

    class QuestionVote < ActiveRecord::Base
      validates :user_id, :uniqueness => { :scope => :question_id }
    end
    

提交回复
热议问题