我想为商店实现营业时间模式。
目前我有
Class Store
has_one :opening_table
#return opening_times as an array
def opening_times
self.build_opening_table unless opening_table
(0..6).collect{ |i|
[opening_table.read_attribute("open_#{i}"),opening_table.read_attribute("close_#{i}") ]
}
end
def opening_times=(attr)
self.build_opening_table unless opening_table
i=0
attr.each do |el|
opening_table.attributes= {"open_#{i}".to_sym => el[0]}
opening_table.attributes= {"close_#{i}".to_sym => el[1]}
i=i+1
end
end
并且OpeningTable 有字段open_0、close_0 表示周一开盘和收盘时间
create_table :opening_tables do |t|
t.references :advertisement, :null=>false
(0..6).each do |i|
t.integer "open_#{i}"
t.integer "close_#{i}"
end
t.timestamps
end
我对这个解决方案感到不舒服和灵活,但我不知道以更好的方式实施它