根据另一个属性值验证一个属性的值

Validate the Value of One Attrribute Based on Another Attribute's Value

如果给定属性存在,您如何强制存在另一个属性?

我有一个具有国家和州属性的用户模型。我想验证用户何时尝试注册,他没有绕过客户端验证并使用 params[:country]="canada"params[:state]="new_york".

提交恶意请求

示例实现:

validates :state, inclusion: { in: [ON, BC, AB],
    message: "%{state} is not valid" }, if: :country_is_canada?

def country_is_canada?
  country == 'Canada'
end