ruby-on-railsthor

undefined method `find_each' for Thor::Group:Class


I have written a Thor task that's supposed to iterate through all Users leveraging find_each. But I get this error message: undefined method find_each for Thor::Group:Class`

Why isn't find_each available here? Running User.find_each { |u| puts u.name } in rails console works.

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

Solution

  • Change User to ::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