我想每天和整個期間的總金額總和。Rails:如何總計每天和整個期間的總金額
我還想顯示每種貨幣的金額。
顯示圖像如下。
Day 1
event_title1-1 USD 1.11
event_title1-2 -
event_title1-3 EUR 2.22
event_title1-4 USD 3.33
event_title1-5 -
Today's total
USD 4.44
EUR 2.22
-------------
Day 2
event_title2-1 EUR 4.44
event_title2-2 SGD 5.55
event_title2-3 -
event_title2-4 -
event_title2-5 EUR 6.66
Today's total
EUR 11.10
SGD 5.55
===========
Grand Total
USD 4.44
EUR 15.54
SGD 5.55
Althouhg我應該使用諸如group
或sum
等等,我coundn't瞭解如何將它們應用到我的代碼。
所以,如果你能給我任何建議,將不勝感激。
我的模型
class Schedule
has_many :days, inverse_of: :schedule, dependent: :destroy
end
class Room
belongs_to :schedule, inverse_of: :rooms
has_many :events, inverse_of: :room, dependent: :destroy
end
class Event
belongs_to :room, inverse_of: :events
has_one :schedule, autosave: false, through: :room
end
schema.rb
create_table "scedules", force: :cascade do |t|
t.string "title"
t.integer "user_id"
...
end
create_table "rooms", force: :cascade do |t|
t.string "room"
t.integer "scedule_id"
t.integer "day", default: 1
...
end
create_table "events", force: :cascade do |t|
t.time "start_at"
t.time "end_at"
t.string "title"
t.integer "room_id"
t.string "currency"
t.decimal "amount"
...
end
schedules_controller.rb
...
def show
@schedules = Schedule.find(params[:id])
...
end
...
點意見\時間表\ show.html.erb
<%= render @schedules %>
意見\時間表\ _schedule.html.erb
<% schedule.rooms.each_with_index do |r, idx| %>
<div class="day">Day <%= idx + 1 %></div>
<% r.events.each do |e| %>
...
<%= e.title %>
<% if e.currency.present? && e.amount.present? %>
<div align="right">
<% if e.currency.to_i == 1 %>
USD
<% elsif e.currency.to_i == 2 %>
EUR
<% elsif e.currency.to_i == 3 %>
SGD
...
<% end %>
<%= e.amount %>
</div>
<% end %>
<% end%>
...
<% end%>
<% end%>
如果你能給我一些建議將不勝感激。