I'm not sure if there's an elegant way to do this, I'm afraid. The only option I can think of is something like this in your index definition:
(2008..Date.today.year).each do |year|
has "SUM(CASE WHEN YEAR(posts.created_at) = #{year} THEN rating ELSE 0 END",
as: "total_rating_for_#{year}".to_sym, type: :float
end