- 1 year ago 79
Ruby Question

How to create conditional for attribute that equals current month?

How to only show

if there is a challenge that has a
, "deadline"
, set for the current month?


@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] }


<% 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.
<% end %>

Answer Source

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]]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download