In regards to QueryObjects in this article: 7 Patterns to Refactor Fat ActiveRecord Models | Code Climate, what’s a good argument for why this technique is better than having a concern that deals only with Query methods within a given model? Based on the example below, I prefer using a Concern. Furthermore, if we have 10 of these QueryClasses, that’s a lot more code than 10 methods in the concern.
Here’s the Code Climate article’s example
class AbandonedTrialQuery
def initialize(relation = Account.scoped)
@relation = relation
end
def find_each(&block)
@relation.where(plan: nil, invites_count: 0).find_each(&block)
end
end
And used like
AbandonedTrialQuery.new.find_each do |account|
account.send_offer_for_support
end
Using a concern, the code would look like:
module Accounts::FinderMethods
extend ActiveSupport::Concern
module ClassMethods
def self.abandondedTrials
where(plan: nil, invites_count: 0)
end
end
end
and used like
Accounts.abandonedTrials.each do |account|
account.send_offer_for_support
end