1

検証する月の無効な日を持つ DateTimes が必要です。Rails 4でMongoid 4alpha2を使用していますが、モデルには

field :date_of_birth, type: DateTime

「1988/02/30」で date_of_birth のコントローラーから通常の「作成」を行うと、モデルは、通常の DateTime のように DateInvalid エラーを取得する代わりに、「1988/03/1」の date_of_birth で保存されます。 new(1988,2,30) は Rails コンソールに表示されます。モペットまたはモンゴイドがDateTimeのRails検証を回避しているかどうかはわかりませんが、他の誰かがこれに遭遇しましたか?

ここにレールログがあります

Started POST "/drivers" for 127.0.0.1 at 2014-01-16 10:42:40 -0500 Processing by DriversController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"LiKx1ZToNVtNL9FAEgyLNNWW7mABy2BPKPwVVcTtXKk=", "driver"=>{"field_worker_name"=>"Field Worker", "hack_number"=>"38924", "first_name"=>"test", "middle_initial"=>"", "last_name"=>"testing", "date_of_birth"=>"1988/02/30", "gender"=>"", "nationality"=>"", "language"=>"", "street"=>"something", "apartment_number"=>"", "city"=>"something", "state"=>"NY", "zip_code"=>"02398", "cell_phone"=>"", "other_phone"=>"", "email"=>"", "dmv_number"=>"", "state_of_residence"=>"", "has_health_insurance"=>"false", "health_plan"=>"", "date_of_recertification"=>""}, "commit"=>"Add Driver"}

MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} runtime: 0.5280ms

MOPED: 127.0.0.1:27017 QUERY database=healthfund_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('52d59524544b38160c000000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.4150ms

MOPED: 127.0.0.1:27017 QUERY database=healthfund_development collection=drivers selector={"hack_number"=>38924} flags=[] limit=-1 skip=0 batch_size=nil fields={:_id=>1} runtime: 1.1000ms

MOPED: 127.0.0.1:27017 INSERT database=healthfund_development collection=drivers documents=[{"field_worker_name"=>"Field Worker", "hack_number"=>38924, "first_name"=>"test", "middle_initial"=>"", "last_name"=>"testing", "date_of_birth"=>1988-03-01 00:00:00 UTC, "gender"=>"", "street"=>"something", "apartment_number"=>"", "city"=>"something", "state"=>"NY", "zip_code"=>"02398", "cell_phone"=>"", "other_phone"=>"", "email"=>"", "nationality"=>"", "language"=>"", "dmv_number"=>"", "state_of_residence"=>"", "has_health_insurance"=>false, "health_plan"=>"", "date_of_recertification"=>nil, "_id"=>38924, "updated_at"=>2014-01-16 15:42:40 UTC, "created_at"=>2014-01-16 15:42:40 UTC}] flags=[] COMMAND database=healthfund_development command={:getlasterror=>1, :w=>1} runtime: 3.4590ms Redirected to http://localhost:3000/drivers/38924 Completed 302 Found in 17ms

4

1 に答える 1

1

Mongoid は日付の解析を MongoDB に任せているようです。MongoDB シェルで確認します。

> new Date('1988/02/30')
ISODate("1988-03-01T08:00:00Z")

これは、 JavaScript のDateコンストラクターにとって完全に許容される動作です。

注: Date が複数の引数を持つコンストラクターとして呼び出される場合、値がその論理範囲よりも大きい場合 (たとえば、月の値として 13 が提供されたり、分の値として 70 が提供されたりする場合)、隣接する値が調整されます。たとえば 、new Date(2013,13,1)は と同等でnew Date(2014,1,1)、どちらも の日付を作成します2014-01-01。他の値についても同様:はどちらも の日付を作成するものnew Date(2013,2,1,0,70)と同等です。new Date(2013,2,1,1,10)2013-02-01T01:10:00

したがって、MongoDB に関する限り、1988/02/301988 年プラス 2 か月プラス 30 日であり、1988 年以降は閏年であり、2 月プラス 30 日は 3 月 1 日です。

:type => Dateいずれにせよ、生年月日には aを使用する必要があります。もちろん、Date同じ「JavaScript のように扱う」動作があります。

class M
  include Mongoid::Document
  field :d, :type => Date
end

m = M.create(:d => '1988/02/30')
m.d
# Tue, 01 Mar 1988

だからあまり役に立ちません。

バグを報告して、Mongoid の人々がこれについてどう思うかを見ることができます。それまでの間、より厳密な解析が必要な場合は、自分で行うことができます。

field :date_of_birth, :type => Date

def date_of_birth=(date)
  super(Date.parse(date))
end

次に、 と言うと例外が発生しますModel.create(:date_of_birth => '1988/02/30')

于 2014-01-17T00:39:34.740 に答える