我正在为Rails中的可数资源租赁编写预订系统。资源所有者希望看到随时间预订的总资源量百分比。
calendar_for gem似乎是不错的选择,除了我找不到在一段时间内显示这个百分比的方法。
假设,总资源为100:
我希望看到预订容量
下面的代码是着名的RailsCast#213课程的复制品。
index_calendar.html.erb
<%= @date.prev_month.strftime("%B %Y") %>
<%= calendar_for(@bookings, :year => @date.year, :month => @date.prev_month.month) do |calendar| %>
<%= calendar.head('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat') %>
<% calendar.day(:day_method => :from_date) do |date, bookings| %>
<%= date.day %>
<ul>
<% for booking in bookings %>
<li><%= link_to h(booking.capacity), booking %></li>
<% end %>
</ul>
<% end %>
<% end %>
替代是:
<li><%= link_to h(booking.total_capacity(date)), booking %></li>
我尝试结合 booking.rb 两种方法:
def capacity
@a=Resource.find_by_id(self.resource_id).units
[(self.units/@a*100).to_s + "%"].join
end
def total_capacity(day)
@wall=Booking.joins(:resource).
where("#{day}<=till_time AND from_time>#{day}").sum(:units)
end
预订模式包含以下字段:
资源模型有
谢谢, NIK
答案 0 :(得分:0)
我的问题的解决方案是作为这个问题的答案: Fill object from array
<强> booking_days / index_calendar.html.erb 强>
<%= @date.prev_month.strftime("%B %Y") %>
<%= calendar_for(@booking_days, :year => @date.year, :month => @date.month) do |calendar| %>
<%= calendar.head('Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat') %>
<% calendar.day(:day_method => :day) do |date, booking_days| %>
<%= date.day %>
<ul>
<% for booking_day in booking_days %>
<li><%= link_to h(booking_day.value), booking_day %></li>
<% end %>
</ul>
<% end %>
<% end %>
这是使用 booking_days_controller.rb
备份的def index_calendar
@date = params[:month] ? Date.strptime(params[:month],"%Y-%m") : Date.today
dstart=@date.prev_month.beginning_of_month
dend=@date.next_month.end_of_month
rid=Resource.find_by_user_id(session[:user_id]).id
sql = ["select @row := @row + 1 as id, ts.dat as day, sum(bookings.value) as value " +
"from (select adddate('#{dstart}', num1000.i) as dat " +
"from num1000 where adddate('#{dstart}', num1000.i) <= '#{dend}') as ts, resources, " +
"bookings JOIN (SELECT @row := 0) r " +
"where bookings.resources_id=resources.id AND resources.id=#{rid} AND " +
"adddate(bookings.from_time,-1)<=ts.dat AND ts.dat<=bookings.till_time group by ts.dat"].join
@booking_days = BookingDay.find_by_sql(sql)
respond_to do |format|
format.html # index_calendar.html.erb
format.json { render json: @booking_days }
end
end
Booking_days“在运行中”创建,不会存储到DB中。
Booking_day的结构是{:id,:value,:day}。
Booking对象存储在DB中,并具有以下结构{:user_id,:resource_id,:from_time,:to_time,:value}。它是通过booking_day对象在日历中显示的所有信息的基础。
num1000是i = 0 ... 999
的表的Nik