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

前端 未结 6 2052
梦如初夏
梦如初夏 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:08

    validates_uniqueness_of :user_id, :scope => [:question_id]
    

    if you needed to include another column (or more), you can add that to the scope as well. Example:

    validates_uniqueness_of :user_id, :scope => [:question_id, :some_third_column]
    

提交回复
热议问题