简体   繁体   English

仅在Rails.env.production中进行某些验证?

[英]Certain validations only in Rails.env.production?

I would like to only allow certain validations within production vs other environments for an app. 我只想在某个production环境与其他环境中进行某些验证。

For example, I have tried to add: 例如,我尝试添加:

if Rails.env.production?
  validates :email, :uniqueness => true
  validates :phone, :uniqueness => true
end

However this will not work. 但是,这将不起作用。 How can one add validations only to specific environment modes? 如何仅将验证添加到特定的环境模式?

Try this - 尝试这个 -

validates :email, :uniqueness => true, :if => lambda{ Rails.env.production?}
validates :phone, :uniqueness => true, :if => lambda{ Rails.env.production?}

完全同意评论,但如果您确实想要这样做,请尝试:if选项

validates_uniqueness_of :email, :if => Rails.env.production?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM