Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord rails 3模型中的验证后处理_Activerecord_Ruby On Rails 3 - Fatal编程技术网

Activerecord rails 3模型中的验证后处理

Activerecord rails 3模型中的验证后处理,activerecord,ruby-on-rails-3,Activerecord,Ruby On Rails 3,只是想知道是否有人知道一种根据:link attr是否有效来运行\u验证逻辑的方法 validates :link, :uri_check => { :schemes => [:ftp, :http, :https] } after_validation :initialize_dependents, :on => :create def initialize_dependents #post validation logic here end 因此,与其:on

只是想知道是否有人知道一种根据:link attr是否有效来运行\u验证逻辑的方法

validates :link, :uri_check => { :schemes => [:ftp, :http, :https] } 

after_validation :initialize_dependents, :on => :create

def initialize_dependents 
  #post validation logic here
end 
因此,与其:on=>:create,不如

after_validation :initialize_dependents, :if => link.valid?
问题是我只能访问字符串而不能访问对象

任何帮助都将不胜感激


谢谢

试试
:如果像这样:

:if => Proc.new { --code here-- }


明白了!只需要创建一个观察者

class LinkObserver < ActiveRecord::Observer

  after_validation(model)
   #logic here
  end

end
class-LinkObserver
Hi Zabba,谢谢你的回复。是否有方法访问验证逻辑的结果。在我的控制器中,我可以使用@link.vaild?在不再次验证模型中的(字符串)链接的情况下,如何应用此方法?
class LinkObserver < ActiveRecord::Observer

  after_validation(model)
   #logic here
  end

end