Last active
August 29, 2015 14:13
-
-
Save rebelweb/92ec14b7fa641b1f9fb4 to your computer and use it in GitHub Desktop.
Simple Archive for Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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