0
有時我不需要在我的應用程序中簡單地驗證smth,但也可以在驗證之前/之後對其進行更改。 也就是說改變驗證的地方在哪裏?
class Channel < ActiveRecord::Base
validate :validate_url
...
private
def validate_url
url = "rtmp://#{url}" if server_url[0..6] != "rtmp://" #alter cause need this prefix
unless /rtmp:\/\/[a-z0-9]{1,3}\.pscp\.tv:80\/[a-z0-9]\/[a-z0-9]{1,3}\//.match url
errors.add(:url, "...")
end
end
end
或不服這樣
class Channel < ActiveRecord::Base
validate :validate_restreams
...
private
def validate_restreams
self.left_restreams = self.left_restreams - self.restreams #to be sure there's no intersections
end
end
但我覺得這是不是這樣的事情一個正確的地方,所以我需要知道什麼是做正確的方式嗎?
哦,這不是我希望得到答案。我的意思是改變價值不是你通常期望從驗證者那裏得到的,對嗎?所以,也許還有另一種方法可以做到這一點?.. – Ngoral