我通过我的网络应用程序养成了良好的习惯。
致电<%= habit.current_level %>
会显示您习惯的当前水平。
这会列出习惯的所有date_missed
,但我们如何编辑它以仅显示date_missed
的{{1}}?
习惯/ _habit
current_level
<% habit.levels.each do |level| %>
<% level.missed_dates.each do |missed_dates| %>
<% if missed_dates.date_missed.present? %>
<%= missed_dates.date_missed %>
<% end %>
<% end %>
<% end %>
来自 habit.rb
current_level
习惯def current_level
return 0 unless date_started
def committed_wdays
committed.map do |day|
Date::ABBR_DAYNAMES.index(day.titleize)
end
end
def n_days
((date_started.to_date)..Date.yesterday).count do |date|
committed_wdays.include? date.wday
end - self.real_missed_days
end
case n_days
when 0..9
1
when 10..24
2
when 25..44
3
when 45..69
4
when 70..99
5
else
6
end
end
级别,has_many
错过日期
MissedDatesController
has_many
def create
habit = Habit.find(params[:habit_id])
habit.missed_days = habit.missed_days + 1
habit.save!
level = habit.levels.find(params[:level_id])
level.missed_days = level.missed_days + 1
if level.missed_days == 3
level.missed_days = 0
level.days_lost += habit.calculate_days_lost + 2
end
level.save!
head :ok # this returns an empty response with a 200 success status code
@missed_date = level.missed_dates.new(missed_date_params)
@missed_date.save
end
答案 0 :(得分:0)
而不是迭代所有levels
只是将current_level
直接传递到内部块:
<% habit.current_level.missed_dates.each do |missed_dates| %>
<% if missed_dates.date_missed.present? %>
<%= missed_dates.date_missed %>
<% end %>
<% end %>