I want to use to_csv method in a chain like this Company.where(created_at: '2010-01-01').limit(20).to_csv
To do so, I use current_scope and it works fine. But when calling the same method like this Company.to_csv it complains because current_scope is nil.
Right now I'm using (current_scope || where(true)) which does the trick for me, but maybe there's a "proper" way to do it?
def self.to_csv
CSV.generate do |csv|
(current_scope || where(true)).includes(:_address).each do |company|
csv << company.attributes
end
end
end
Ps. I know I could just use current_scope and call Company.all.to_csv, but this is not what's this question is about.