私のデータベースには、次の構造の30k以上のドキュメントがあります。
{
...,
"hours" : {
"holiday" : {
"New Years" : "Call",
"Easter" : "Closed",
"Memorial Day" : "Standard",
"Independence Day" : "Standard",
"Labor Day" : "Standard",
"Thanksgiving" : "Closed",
"Day After Thanksgiving" : "Call",
"Christmas Eve" : "Call",
"Christmas" : "Closed",
"New Years Eve" : "Call"
},
"standard" : {
"mon" : [{
"open" : "8:00 am",
"close" : "11:00 pm"
}],
"tue" : [{
"open" : "8:00 am",
"close" : "11:00 pm"
}],
"wed" : [{
"open" : "8:00 am",
"close" : "11:00 pm"
}],
"thu" : [{
"open" : "8:00 am",
"close" : "11:00 pm"
}],
"fri" : [{
"open" : "8:00 am",
"close" : "11:00 pm"
}],
"sat" : [{
"open" : "8:00 am",
"close" : "11:00 pm"
}],
"sun" : [{
"open" : "8:00 am",
"close" : "10:00 pm"
}]
}
},
...
}
私はphp主導のサイトから完全な書き直しをしています。私は頭を悩ませてきましたが、business.hours ["standard"]["mon"]を適切に設定できるフォームフィールドを作成する方法を思いつくことは絶対にできません。このサイトでは、昼食のために閉店する企業などのために、1日に複数の開店/閉店エントリを提供しています。その属性は[{"open": "8:00 am"、 "close": "12:00 pm}、 {"open": "1:00 pm"、 "close":"5:00pm}]このような場合。エントリーごとに2つの選択ボックスを提供したいと思います。1つはオープンタイム用、もう1つはクローズタイム用です。
これが私のモデルです。私が自分のフォームに結び付ける方法を理解しようとしていたいくつかのゲッター/セッターを含みます...失敗しました。
class Business
include Mongoid::Document
include Mongoid::Timestamps
field :ad, type: String
field :address, type: String
field :city, type: String
field :claimed, type: Boolean
field :coupons, type: String
field :created_at, type: DateTime
field :extra_services, type: Array
field :hours, type: Hash
field :name, type: String
field :organization, type: String
field :permanently_closed, type: Boolean, :default => false
field :phone, type: String
field :state, type: String
field :tags, type: Array
field :unit, type: String
field :updated_at, type: DateTime
field :website, type: String
field :zip, type: String
attr_accessible :ad, :address, :city, :claimed, :coupons, :created_at, :updated_at, :extra_services, :hours,
:name, :organization, :permanently_closed, :phone, :state, :tags, :unit, :website, :zip
index({address: 1, unit: 1, city: 1, state: 1, zip: 1, organization: 1}, {unique: true, name: "address_unique_index"})
after_initialize do |b|
b.hours = Hash.new unless b.hours
b.hours["holiday_hours"] = {"New Years" => "Call",
"Easter" => "Call",
"Memorial Day" => "Call",
"Independence Day" => "Call",
"Labor Day" => "Call",
"Thanksgiving" => "Call",
"Day After Thanksgiving" => "Call",
"Christmas Eve" => "Call",
"Christmas" => "Call",
"New Years Eve" => "Call"} unless b.hours["holiday_hours"]
b.hours["standard_hours"] = Hash.new unless b.hours["standard_hours"]
end
def holiday_hours
hours["holiday_hours"] if hours["holiday_hours"]
end
def holiday_hours=(hours)
self.hours["holiday_hours"] = hours if hours.present?
end
def holiday_hours_for(holiday)
hours["holiday_hours"][holiday.to_s] if hours["holiday_hours"][holiday.to_s]
end
def update_holiday_hours_for(holiday, hours_text)
self.hours = Hash.new unless hours
self.hours["holiday_hours"] = Hash.new unless hours["holiday_hours"]
self.hours["holiday_hours"][holiday.to_s] = hours_text.to_s
end
def standard_hours
hours["standard_hours"] if hours["standard_hours"]
end
def standard_hours_for(day)
hours["standard_hours"][day.to_s] if hours["standard_hours"][day.to_s]
end
end
ありがとう!