gpt4 book ai didi

ruby-on-rails - rails NoMethodError : private method on private method?

转载 作者:数据小太阳 更新时间:2023-10-29 08:58:39 26 4
gpt4 key购买 nike

我正在尝试使用 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 NoMethodError : private method on private method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42227971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com