为什么我无法通过类封装方法访问下面代码中的check_url
私有方法?无法从类内部访问私有方法?
class Link < MyModel
# whitelist fields for security
attr_accessible :is_valid,
:url
# validations
validates :is_valid, :inclusion => { :in => [true, false] }
validates :url, :presence => true
# ===============================================================
# = class methods (accessible from outside without an instance) =
# ===============================================================
class << self
def is_url_valid(url)
unless link = Link.find_by_url(url)
link = Link.create!(:url => url, :is_valid => check_url(url))
end
link.is_valid
end
end
# =====================
# = Private functions =
# =====================
private
# Checks if url is broken
def check_url(url)
# do stuff
end
end
PS。我也尝试使用self.check_url
,它没有奏效。
那么这就是代码已经 - 不起作用。我试过'self.check_url'和'check_url' – user1049097
更新了我的答案。 – miaout17
谢谢。这有效 - 我不知道我不能从基类访问私有函数,除非它包含在自引用类中。 – user1049097