我正在尝试使用 Rails 控制台迭代所有 Ad
模型。我希望每个广告执行一个名为 extract_targets
的私有(private)方法:
Ad.all.each do |ad|
ad.extract_targets
end
尽管当我在循环时收到以下错误时:
NoMethodError: private method `extract_targets' called for #<Ad:0x000000071123b0>
这是一个私有(private)方法,但我从每个广告中调用它,所以我不知道为什么它会引发异常?
private
def extract_targets
normal_name = campaign.name.sub(/^www\./, '')
links = page.css('a').map { |link| URI(link['href']) }.select {|link| link.host.sub(/^www\./, '').end_with? normal_name }
location = links.first.path
location = '/' if location.empty?
location.normalize_path!
update_column :target_id, campaign.targets.find_or_create_by(location: location).id
end
最佳答案
您不能使用显式接收者调用私有(private)方法。因此,要么将其公开,要么使用send
,或者调用 Ad
上的另一个方法来调用此方法。
关于ruby-on-rails - Rails NoMethod错误: private method on private method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42227971/