I’m doing a challenge today where:
我今天在挑战:
“A dose must have a description, a cocktail and an ingredient, and [cocktail, ingredient] pairings should be unique.”
“剂量必须有说明,鸡尾酒和成分,并且[鸡尾酒,成分]配对应该是唯一的。”
To do so, we need to use uniqueness validator with scope:
为此,我们需要对范围使用唯一性验证器:
class Dose < ApplicationRecord
belongs_to :cocktail
belongs_to :ingredient
validates :description, presence: true validates :cocktail, uniqueness: { scope: :ingredient }end
Also would work with
也将与
class Dose < ApplicationRecord
belongs_to :cocktail
belongs_to :ingredient
validates :description, presence: true validates_uniqueness_of :cocktail, scope::ingredientend
Flexible, right?.. Loving Ruby on Rails!
灵活,对吗?..喜欢Ruby on Rails!