AnthonyGalli.com AnthonyGalli.com - 3 months ago 13
Ruby Question

How to create conditional for attribute that equals current month?

How to only show

<b>September</b>
if there is a challenge that has a
deadline
,
t.date "deadline"
, set for the current month?

controller

@challenges = current_user.challenges.unaccomplished.order("deadline ASC").select{ |challenge| challenge.deadline.month == Date.current.month }
@current_month = (@challenges).group_by { |t| [t.deadline.year, t.deadline.month] }


view

<% if @current_month[Date.current.month].present? %> # This is not currently being triggered even though their is a challenge with a deadline in the current month.
<b>September</b>
<% end %>

Answer

You've defined the group key as a tuple [year, month]. And you need to use exactly the same format when you pick the challenge from the list. I.e.

@current_month[[Date.current.year, Date.current.month]]