特定の月番号のすべての宣言を検索したい。「declaration_date」は日時属性です。私はこれを持っていますが、失敗します、何かアイデアはありますか?
declaration = Declarations.all(:conditions => ['declaration_date.strftime("%-m") = 2')
特定の月番号のすべての宣言を検索したい。「declaration_date」は日時属性です。私はこれを持っていますが、失敗します、何かアイデアはありますか?
declaration = Declarations.all(:conditions => ['declaration_date.strftime("%-m") = 2')
strftimeが文字列を返すことを期待しており、整数2と比較しています。文字列「2」と比較してみてください。
declaration = Declarations.all(:conditions => ["'declaration_date.strftime("%-m") = '2'")
特定の年の1か月以内に宣言を行いますか?もしそうなら、このようなことを試してください:
time_range = (DateTime.new(2001,2,3) - 1.month)..DateTime.new(2001,2,3)
declaration = Declaration.where( 'declaration_date' => time_range )
この手法に従って、数年にわたって同じ月に宣言を取得できます。
Active RecordQueryInterfaceドキュメントのセクション11.3も参照してくださいhttp://guides.rubyonrails.org/active_record_querying.html