Q: How to sort model by sum of attributes? I have an Artist model represented by serializer
class ArtistSerializer < ActiveModel::Serializer
  attributes :id,
             :name,
             :followers_total_count

  def followers_total_count
    [
      object.facebook_like_count,
      object.soundcloud_like_count,
      object.instagram_like_count,
      object.twitter_like_count,
      object.mixcloud_like_count,
      object.youtube_like_count
    ].map(&:to_i).reduce(:+)
  end
end

How to sort Artist.all by sum of all providers like count.
Example:
Artist1


*

*facebook_like_count => 100

*twitter_like_count => 50


Artist2


*

*instagram_like_count => 20

*twitter_like_count => 1000


order should be [Artist2, Artist1]

A: Assuming :followers_total_count is an integer, you could try this
Artist.order(:followers_total_count, :desc)

