Skip to content

Instantly share code, notes, and snippets.

@flada-auxv
Created November 7, 2012 12:05
Show Gist options
  • Save flada-auxv/4031106 to your computer and use it in GitHub Desktop.
Save flada-auxv/4031106 to your computer and use it in GitHub Desktop.
月単位でのページネーション
# -*- coding: utf-8 -*-
<!-- beginning_of_monthはyyyy-mm-ddで取得するが、
ddが入ってるのがクエリで見えてしまうとおかしいし不要な情報でもあるので加工してる。-->
<% Work.all.map {|w| w.date.beginning_of_month}.uniq.sort.each do |m| -%>
<%= link_to_unless_current m.month, month: m.to_s.sub!(%r[\-\d{2}$],"") %>&nbsp;
<% end %>
# -*- coding: utf-8 -*-
class Work < ActiveRecord::Base
# その月のレコードだけを取得する
# Stringの形式はyyyy-mm
# Time.parseはyyyy-mm-ddを与えてやる必要がある
# Time#all_monthは月初めから終わりのrangeオブジェクトを返す
scope :by_month, lambda {|arg|
arg = Time.parse(arg << "-01") if arg.instance_of?(String)
where(date: arg.all_month).order("date")
}
end
# -*- coding: utf-8 -*-
class WorksController < ApplicationController
# GET /works
def index
@works = @user.works.by_month(params[:month] || Time.now)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment