[前][次][番号順一覧][スレッド一覧]

rails:2673

From: Shin Morichika <ofl@c...>
Date: Sat, 10 May 2008 17:51:15 +0900
Subject: [rails:2673] Re: script/console で OK な SQL が、 Class.find(〜) では「 Couldn't find Class without an ID 」エラー



> --------------------
> # notes_controller.rb
>
>   def weekly
>     @note_a_week = [0, -1, -2, -3, -4, -5, -6]
>     @weekly = []
>
>     for day in @note_a_week do
>       if day == 0
>         res = Note.find(:all, :conditions => ["created_on
> = ?", Time.today])
>       else
>         res = Note.find(:all, :conditions => ["created_on
> = ?", Time.today + day])
>       end
>       @weekly << res
>     end
>   end
>

なぜdayを0とそれ以外に分ける必要があるのかとか、 
Array#eachやinjectを使った方が
Rubyっぽいかなとか多少疑問があるのですが、、

実際に自分の環境でコードを試したところ発行されたSQLは

   Note Load (0.001214)   SELECT * FROM notes WHERE (created_on =  
'2008-05-10 00:00:00')
   Note Load (0.000576)   SELECT * FROM notes WHERE (created_on =  
'2008-05-09 23:59:59')
   Note Load (0.000552)   SELECT * FROM notes WHERE (created_on =  
'2008-05-09 23:59:58')
   Note Load (0.000526)   SELECT * FROM notes WHERE (created_on =  
'2008-05-09 23:59:57')
   Note Load (0.000523)   SELECT * FROM notes WHERE (created_on =  
'2008-05-09 23:59:56')
   Note Load (0.000520)   SELECT * FROM notes WHERE (created_on =  
'2008-05-09 23:59:55')
   Note Load (0.000515)   SELECT * FROM notes WHERE (created_on =  
'2008-05-09 23:59:54')

でした。
created_onのデータがdate形式(2008-05-10)であるならば
Time.todayではなくてDate.todayにしておく必要がありそうです。




-- 
森近 真
uy4s-mrck@a...



--
ML: rails@r...
使い方: http://QuickML.com/

[前][次][番号順一覧][スレッド一覧]

      2670 2008-05-09 17:50 [moronatural@g...    ] [ANN] Rails勉強会@東京を5/18(日)に大森で実施します
      2671 2008-05-10 08:30 ┗[roppongitoppogi@y...] script/console で OK な SQL が、 Class.find(〜) では「 Couldn't find Class without an ID 」エラー 
->    2673 2008-05-10 10:51  ┗[ofl@c...            ]                                     
      2674 2008-05-10 11:37   ┗[roppongitoppogi@y...]                                   
      2675 2008-05-10 11:44    ┗[roppongitoppogi@y...]                                 
      2676 2008-05-10 12:23     ┗[aliasdice@m...      ]                               
      2677 2008-05-10 14:39      ┗[roppongitoppogi@y...]                             
      2678 2008-05-10 18:55       ┗[aliasdice@m...      ]