2014-05-20 41 views
0

我想在模型文件編寫這樣的邏輯地址字段執行組生效addr_state,:addr_city和:addr_zip也進入了。如何爲在軌

現在,我已經是這個,但不容易進入郵編我可以通過:

[:addr_aptno, :addr_zip].each do |n| 
    validates n, numericality: { only_integer: true, 
           :greater_than => 0, 
           :allow_blank => true 
           } 

回答

0

您可以使用validatesif選項:

validates :addr_state, presence: true, if: :addr_streetno 
validates :addr_city, presence: true, if: :addr_streetno 
validates :addr_zip, presence: true, if: :addr_streetno 
validates :addr_aptno, numericality: { only_integer: true, greater_than: 0 }, allow_blank: true 
validates :addr_zip, numericality: { only_integer: true, greater_than: 0 }, allow_blank: true