script/generate integration_test
続きを読む
2009年07月13日
2009年07月12日
2009年07月11日
2009年07月09日
2009年07月07日
2009年07月06日
2009年07月05日
2009年07月04日
2009年07月02日
2009年07月01日
2009年06月30日
2009年06月28日
【1日1Rails】前月・翌月を取得する
Ruby 1.8.7
Rails 2.3.2
# Dateでやってますが、Timeでも動きます
# 先月=last_month, 翌月=next_month
>> date = Date.new(2009, 1, 1)
=> Thu, 01 Jan 2009
>> date.last_month
=> Mon, 01 Dec 2008
>> date.next_month
=> Sun, 01 Feb 2009
# 2ヶ月後、2ヶ月前
>> date.months_ago(2)
=> Sat, 01 Nov 2008
>> date.months_since(2)
=> Sun, 01 Mar 2009
# monthをyearにすれば、年の扱い
>> date.next_year
=> Fri, 01 Jan 2010
>> date.last_year
=> Tue, 01 Jan 2008
>> date.years_ago(2)
=> Mon, 01 Jan 2007
>> date.years_since(2)
=> Sat, 01 Jan 2011
# 標準関数でやるとこんな感じ
>> date = Date.new(2009, 1, 1)
>> date << 1
=> 20081201
# ちなみに3月31日の1ヶ月前2月28日
>> date = Date.new(2009, 3, 31)
>> date << 1
=> 20090228
Rails 2.3.2
# Dateでやってますが、Timeでも動きます
# 先月=last_month, 翌月=next_month
>> date = Date.new(2009, 1, 1)
=> Thu, 01 Jan 2009
>> date.last_month
=> Mon, 01 Dec 2008
>> date.next_month
=> Sun, 01 Feb 2009
# 2ヶ月後、2ヶ月前
>> date.months_ago(2)
=> Sat, 01 Nov 2008
>> date.months_since(2)
=> Sun, 01 Mar 2009
# monthをyearにすれば、年の扱い
>> date.next_year
=> Fri, 01 Jan 2010
>> date.last_year
=> Tue, 01 Jan 2008
>> date.years_ago(2)
=> Mon, 01 Jan 2007
>> date.years_since(2)
=> Sat, 01 Jan 2011
# 標準関数でやるとこんな感じ
>> date = Date.new(2009, 1, 1)
>> date << 1
=> 20081201
# ちなみに3月31日の1ヶ月前2月28日
>> date = Date.new(2009, 3, 31)
>> date << 1
=> 20090228
2009年06月27日
【1日1Rails】月の末日を取得する
Ruby 1.8.7
Rails 2.3.2
# end_of_monthで取れる
Date.today.end_of_month #=> Tue 30 Jun 2009
# Timeだと末日の最終秒が取れる
Time.now.end_of_month #=> Tue Jun 30 23:59:59 +0900 2009
# Rubyの標準関数のみでやろうとすると、こんな感じ
Date.new( Date.today.year, Date.today.month, -1 ) #=> Tue 30 Jun 2009
Rails 2.3.2
# end_of_monthで取れる
Date.today.end_of_month #=> Tue 30 Jun 2009
# Timeだと末日の最終秒が取れる
Time.now.end_of_month #=> Tue Jun 30 23:59:59 +0900 2009
# Rubyの標準関数のみでやろうとすると、こんな感じ
Date.new( Date.today.year, Date.today.month, -1 ) #=> Tue 30 Jun 2009
【1日1Rails】SQLを直接実行する
# Railsで直接SQLを発行する
# エンコード指定を打ちたい時、
# ActiveRecordの実行速度がボトルネックになる時などに使用
# SELECTも実行できる
rs = ActiveRecord::Base.connection.execute( "select * from hoge" )
rs.each { | item |
puts item[0]
}
# エンコード指定を打ちたい時、
# ActiveRecordの実行速度がボトルネックになる時などに使用
# SELECTも実行できる
rs = ActiveRecord::Base.connection.execute( "select * from hoge" )
rs.each { | item |
puts item[0]
}