【发布时间】:2012-10-30 05:20:49
【问题描述】:
我有以下功能很好用
def holiday_hours_for(holiday)
hours["holiday_hours"][holiday.to_s] if hours["holiday_hours"][holiday.to_s]
end
我只是在学习虚拟属性,在弄清楚这个函数的 setter 版本时遇到了一些麻烦。我该如何实现这个功能...
def holiday_hours_for(holiday)=(hours)
self.hours["holiday_hours"][holiday.to_s] = hours 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
【问题讨论】:
标签: ruby-on-rails-3 mongoid virtual-attribute