2012-03-02 6 views
11

Rails 3.2アプリでは、due_dateと等しい今日のすべてのイベント項目を返すようにクエリが定義されています。明日の日付を定義するRailsの方法は何ですか?

@due_today = Event.where(:due_date => Time.now.beginning_of_day..Time.now.end_of_day) 

これを修正して、今日と明日の予定をすべて返すようにしたいと考えています。

これを行うにはどのような方法が最適ですか?

私は私に利用できるいくつかのオプションがあります知っている:

Date.tomorrow 
Date.current.tomorrow 
Date.now.tomorrow 
DateTime.now.tomorrow.to_date 
Time.now.tomorrow.to_date 
Date.current+1 

私は他の人があることを確認しています。これらはすべて交換可能ですか?パフォーマンスに違いはありますか?異なるアプローチに関連する問題はありますか?私はどんな提案が最高の方法を歓迎するでしょう。

その他の名誉について:私は、due_dateをToday HH:MMまたはTomorrow HH:MMとして表示したいと思います.HH:MMは時刻です。今日や明日のように日付を表​​示するためにRailsに焼いたメソッドがありますか?または自分のスコープを定義する必要がありますか?

多くの感謝!これらのベンチマーク

+0

をインクリメントしたい場合は、私はあまりパフォーマンスを心配しないでしょう。タイトなループで何千回も日付を計算しないので、パフォーマンスの差はごくわずかです。最も読みやすいものと一緒に行きなさい。 'now'はタイムゾーンを考慮に入れますが、' now'は考慮しません。 – Dennis

答えて

21

、私が取得:

N = 100000 
Benchmark.bmbm do |test| 
    test.report("Date.tomorrow") do 
    N.times do 
     x = Date.tomorrow 
    end 
    end 
    test.report("Date.current.tomorrow") do 
    N.times do 
     x = Date.current.tomorrow 
    end 
    end 
    # test.report("Date.now.tomorrow") # => Coughs up an exception, Date.now doesn't exist! 
    test.report("DateTime.now.tomorrow.to_date") do 
    N.times do 
     x = DateTime.now.tomorrow.to_date 
    end  
    end 
    test.report("Time.now.tomorrow.to_date") do 
    N.times do 
     x = Time.now.tomorrow.to_date 
    end 
    end 
    test.report("Date.current+1") do 
    N.times do 
     x = Date.current+1 
    end 
    end 
    test.report("DateTime.tomorrow") do 
    N.times do 
     x = DateTime.now 
    end  
    end 
end 

結果:

Rehearsal ----------------------------------------------------------------- 
Date.tomorrow     1.640000 0.010000 1.650000 ( 1.662668) 
Date.current.tomorrow   1.580000 0.000000 1.580000 ( 1.587714) 
DateTime.now.tomorrow.to_date 0.360000 0.010000 0.370000 ( 0.363281) 
Time.now.tomorrow.to_date  4.270000 0.010000 4.280000 ( 4.303273) 
Date.current+1     1.580000 0.010000 1.590000 ( 1.590406) 
DateTime.tomorrow    0.160000 0.000000 0.160000 ( 0.164075) 
-------------------------------------------------------- total: 9.630000sec 

            user  system  total  real 
Date.tomorrow     1.590000 0.000000 1.590000 ( 1.601091) 
Date.current.tomorrow   1.610000 0.010000 1.620000 ( 1.622415) 
DateTime.now.tomorrow.to_date 0.310000 0.000000 0.310000 ( 0.319628) 
Time.now.tomorrow.to_date  4.120000 0.010000 4.130000 ( 4.145556) 
Date.current+1     1.590000 0.000000 1.590000 ( 1.596724) 
DateTime.tomorrow    0.140000 0.000000 0.140000 ( 0.137487) 

提案のリストから、DateTime.now.tomorrow.to_dateが高速です。

最後に追加したオプションを確認してください。Dateオブジェクトが返され、カントリーマイルで一番速いです。それはまた、リストから最も人間が読めるものの1つです。

あなたはMySQLの()関数BETWEENを使用する場合、クエリがより速くなるかもしれない、あなたはMYSQLを使用していると仮定すると:

@due_today = Event.where("due_date BETWEEN ? AND ?", DateTime.today, DateTime.tomorrow) 

私はあなたがevents.due_dateや意志BETWEEN場合は、インデックスを持っているのかはわからないが、それでもこれらを使用します。大きなDATAセットを使用するとどちらが速いかを見るために両方をベンチマークする必要があります。

希望しますか?

+0

ありがとう、素晴らしい答え!しかし、今日のDateTimeは 'NoMethodError:privateメソッド 'today'をDateTime:Class'のために返します。なぜこれがどんなアイデアですか?また、私はMySQLよりPostgresを使用していますが、私は 'BETWEEN'演算子が有効であると思います。 –

+0

申し訳ありませんが、私のせいです。私は 'DateTime.now'ではなく' DateTime.today'を呼び出そうとしていました。そして、 'BETWEEN'演算子が動作しています。ありがとう!今日の日付を "Today"としてRailsに表示する方法を知っていますか? –

+0

状況に応じて日付をカスタム形式で表示するように思えます。このブログの投稿をチェックしてください:http://gavinmorrice.com/blog/posts/tagged/strftimeそしてこの要点https://gist.github.com/1957685 – bodacious

11

これはいかがですか?

1.day.from_now 
2.days.from_now 
3.days.from_now 

あなたが特定の時間

1.day.from_now(start_time) # gives a day after the start time 
関連する問題