Use SQL search for date field (it is updated in my example)
years = ActiveRecord::Base.connection.select_all("SELECT DISTINCT DATE_FORMAT(updated_at, '%Y') AS updated_at FROM posts ORDER BY updated_at DESC")
This will return all year values ββin the form
[{"updated_at"=>"2009"}, {"updated_at"=>"2008"}, {"updated_at"=>"1979"}, {"updated_at"=>"1922"}] years.each { |y| puts y['updated_at'] } 2009 2008 1979 1924
source share