Rails: validating a field is present only if another is present

喜夏-厌秋 提交于 2019-12-22 03:23:37

问题


I have a model where there are two fields that can technically be null. The field names are :is_activated and :activated_at. :activated_at is only required if :is_activated is set to true. It does not need to be present if :is_activated is false. What's the appropriate way in Rails to set this validation directly into ActiveRecord?


回答1:


You can use a Proc on the :activated_at validator.

validates :activated_at, :presence, if: Proc.new { |a| a.is_activated? }

Recommended Reading:

  • http://guides.rubyonrails.org/active_record_validations.html#using-a-proc-with-if-and-unless
  • http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html

Finally, you should consider renaming :is_activated to simply :activated. This is considered better practice in Rails. The is_ prefix is used in other languages for boolean attributes because their method names don't support a ? character. Ruby does, and Rails generates ___? methods on boolean attributes. Because of this it's better to just call the attribute :activated and check it with activated?.




回答2:


Non-Boolean Attributes.

If you're not using a Boolean attribute, like is_activated, and want to ensure that one attribute is present when another, non-Boolean attribute is present, you can simplify it:

validates :activated_at, presence: true, if: :activated_by?

activated_by? will return false when null and true otherwise.




回答3:


You could do something like this:

validates :activated_at, presence: true, if: :is_activated?

def is_activated?
  self.is_activated
end

This will only validate :activated_at if the method is_activated? returns true.



来源:https://stackoverflow.com/questions/13750436/rails-validating-a-field-is-present-only-if-another-is-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!