Since current_user.invites
returns an array, you should not use .nil?
because arrays with no items are not nil
, are empty, so this invites
array will be empty ([]
) when has no items, so you can use #empty?
.
Also I removed the second condition because there is no need to check if the invites
is not empty. In other words, if the invites
array is empty, the right side of the OR (||
) condition is not going to be evaluated.
<% if current_user.invites.empty? || (Date.today - current_user.invites.last.created_at) >= 30.days %>
<% end %>
I would also recommend you to extract this code in a custom helper method, so your code will be a little more DRY.
# app/helpers/my_helper.rb
module MyHelper
def invites_created_after_or_none?(user = current_user, days = 30.days)
user.invites.empty? || (Date.today - user.invites.last.created_at) >= days
end
end
<% if invites_created_after_or_none? %>
<% end %>