我编写了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
答案 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