Thor :: Group:Class的未定义方法`find_each'

时间:2018-09-19 09:29:51

标签: ruby-on-rails thor

我编写了Thor任务,该任务应该利用find_each遍历所有用户。但我收到此错误消息: undefined method find_each'代表Thor :: Group:Class`

为什么在这里找不到find_each?在Rails控制台中运行User.find_each { |u| puts u.name }即可。

class CloudImages < Thor
  require "./config/environment.rb"

  desc "save_user_avatar_urls", "Save existing AWS user avatars"
  def save_user_avatar_urls
    User.find_each do |user|
      next if user.avatar_url.blank?

      path      = URI(user.avatar_url).path
      image_url = $DOMAIN + path

      user.update_attribute(:aws_avatar_url, image_url)
    end
  end

end

1 个答案:

答案 0 :(得分:0)

User更改为::User

class CloudImages < Thor
  require "./config/environment.rb"

  desc "save_user_avatar_urls", "Save existing AWS user avatars"
  def save_user_avatar_urls
    ::User.find_each do |user|
      next if user.avatar_url.blank?

      # ...
    end
  end
end