我根据其active_state验证Realty对象,因此如果它处于挂起状态 ,则允许多个字段为blank 。
with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100}, :allow_blank => true realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5}, :allow_blank => true realty.validates :city, :length => {:in => 1..50}, :allow_blank => true realty.validates :description, :length => {:maximum => 8000}, :allow_blank => true realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}, :allow_blank => true end with_options :if => Proc.new { |a| a.active_state != 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100} realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} realty.validates :city, :length => {:in => 1..50} realty.validates :description, :length => {:maximum => 8000} realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9} end唯一的区别是:allow_blank => true选项。
我想让这段代码更干,所以我的尝试是使用普通的验证块一次:
validates :street, :length => {:in => 1..100} validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} validates :city, :length => {:in => 1..50} validates :description, :length => {:maximum => 8000} validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}然后在状态挂起的情况下简单地在所有这些字段上调用一些函数:
with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.allow_blank_of :street, :postalcode, :city, :description, :leasing_costs end类似于所有那些validates_uniqueness_of :x, :y, :z方法。
我找不到适合我需要的功能。 我该如何处理?
I validate Realty objects depending on their active_state, so if it's pending, multiple fields are allowed to be blank.
with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100}, :allow_blank => true realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5}, :allow_blank => true realty.validates :city, :length => {:in => 1..50}, :allow_blank => true realty.validates :description, :length => {:maximum => 8000}, :allow_blank => true realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}, :allow_blank => true end with_options :if => Proc.new { |a| a.active_state != 'pending'} do |realty| realty.validates :street, :length => {:in => 1..100} realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} realty.validates :city, :length => {:in => 1..50} realty.validates :description, :length => {:maximum => 8000} realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9} endThe only difference are those :allow_blank => true options.
I want to make this code more dry, so my attempt would be to use the normal validations block once:
validates :street, :length => {:in => 1..100} validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} validates :city, :length => {:in => 1..50} validates :description, :length => {:maximum => 8000} validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}and then simply call some function on all of those fields in case the state is pending:
with_options :if => Proc.new { |a| a.active_state == 'pending'} do |realty| realty.allow_blank_of :street, :postalcode, :city, :description, :leasing_costs endSimilar to all those validates_uniqueness_of :x, :y, :z methods.
I couldn't find a function to fit my need. How can I approach this?
最满意答案
如果:allow_blank选项采用了proc,那将是很好的,但我认为它没有。 但是,您可以使用:if选项获得相同的结果,因为:allow_blank本质上是一种说法“如果属性为空,则不运行此验证要求”。 所以试试这个:
with_options :if => Proc.new { |a| a.active_state == 'pending' ? a.present? : true } do |realty| realty.validates :street, :length => {:in => 1..100} realty.validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5} realty.validates :city, :length => {:in => 1..50} realty.validates :description, :length => {:maximum => 8000} realty.validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9} end在这种情况下,proc表示......如果active_state为'pending'且属性存在,则执行此验证。 但是,如果active_state不是'pending'则不允许该属性为空,因此始终运行验证。 我希望我能根据你的需要得到正确的逻辑。
From pdobb's answer
However, you can achieve the same result using the :if option since :allow_blank is essentially a way of saying "if the attribute is blank then don't run this validation requirement".
Based on pdobbs explanation what allow_blank => true actually does, I could narrow the validations down to:
validates :street, :length => {:in => 1..100}, :unless => :pending? validates :postalcode, :numericality => {:only_integer => true}, :length => {:in => 4..5}, :unless => :pending? validates :city, :length => {:in => 1..50}, :unless => :pending? validates :description, :length => {:maximum => 8000}, :unless => :pending? validates :leasing_costs, :numericality => {:only_integer => true}, :length => {:in => 1..9}, :unless => :pending? def pending? active_state == "pending" endSo I simply skip all of those validations if the active_state is "pending".
Allot more DRY :)
更多推荐
发布评论