【发布时间】:2016-09-16 01:11:15
【问题描述】:
我对 ROR 很陌生,但我很难做到这一点。
我有一个 Working_hour 模型和一个 Merchant 模型,其中商家 has_many working_hours 和 working_hour 属于 Merchant,如下:
class Merchant < ApplicationRecord
has_many :working_hours, inverse_of: :merchant, dependent: :destroy
accepts_nested_attributes_for :working_hours, reject_if: :all_blank, allow_destroy: true
end
工作时间表
create_table "working_hours", force: :cascade do |t|
t.integer "day"
t.time "open_time"
t.time "close_time"
t.integer "merchant_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["merchant_id"], name: "index_working_hours_on_merchant_id"
end
商家可以在同一天有两个工作时间。
我的看法:
<% @merchant.working_hours.order(:day).each do |wh| %>
<li>
<%= t(:"date.abbr_day_names")[wh.day.to_i] %> :
<%= wh.open_time.to_formatted_s(:time) %> -
<%= wh.close_time.to_formatted_s(:time) %>
</li>
<% end %>
当我在按天排序的视图中显示时,检索到的数据是:
Mon: 10:00-13:00
Mon: 17:00-20:00
Tue: 10:00-13:00
Tue: 17:00-21:00
Wed: 10:00-13:00
如何按天对 working_hours 进行分组并以这种方式显示:
Mon: 10:00-13:00 / 17:00-20:00
Tue: 10:00-13:00 / 17:00-21:00
Wed: 10:00-13:00
我观看了 Railscast 中的 group_by 教程,但我没有用于 working_hour 模型的控制器。有任何想法吗?
【问题讨论】:
-
看起来您的视图中已获得所需的所有数据。你只需要正确地操作它。也许从您的角度发布代码?
-
我编辑了我的问题。本做那件事简单吗?