Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Last active August 29, 2015 14:13
Show Gist options
  • Save rebelweb/92ec14b7fa641b1f9fb4 to your computer and use it in GitHub Desktop.
Save rebelweb/92ec14b7fa641b1f9fb4 to your computer and use it in GitHub Desktop.
Simple Archive for Rails
class Archive < ActiveRecord::Base
#model will be read only!
def self.posts
Post.where('published = ? and published_date >= ? and published_date <= ?', true, Date.civil(a.year, a.month, 1), Date.civil(a.year, a.month, -1))
end
end
class CreateArchives < ActiveRecord::Migration
def up
#this view can be modified easily to archive what you are trying to organize by month/year etc.
ActiveRecord::Base.connection.execute "create view archives as select date_trunc('month',created_at) as archive, count(*) as check_in_count from check_ins GROUP BY date_trunc('month',created_at);"
end
def down
ActiveRecord::Base.connection.execute 'drop view archives'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment