class RuboCop::Cop::Rails::FindEach
User.all.find_each
# good
User.all.each
# bad
@example
change them to use ‘all.find_each` instead.
This cop is used to identify usages of `all.each` and
def autocorrect(node)
def autocorrect(node) each_loc = node.loc.selector ->(corrector) { corrector.replace(each_loc, 'find_each') } end
def ignored_by_find_each?(relation_method)
def ignored_by_find_each?(relation_method) # Active Record's #find_each ignores various extra parameters [:order, :limit, :select].include?(relation_method) end
def method_chain(node)
def method_chain(node) [*node.ancestors, node].map(&:method_name) end
def on_send(node)
def on_send(node) receiver, method, _selector = *node return unless receiver && method == :each _model, preceding_method = *receiver return unless SCOPE_METHODS.include?(preceding_method) return if method_chain(node).any? { |m| ignored_by_find_each?(m) } add_offense(node, node.loc.selector, MSG) end