我试图像这样编码。
这有什么问题?
def topic_button(community)
if !community.topics.order("last_active_at DESC").last.nil? && community.topics.order("last_active_at DESC").last.last_active_at.to_date == Date.current.to_date
'posted today'
else
'no post today'
end
end
答案 0 :(得分:1)
if @community.topics.last_active_at == Date.current.to_date
puts "posted today"
else
puts "no post today"
end
希望有所帮助
答案 1 :(得分:1)
你应该只做一次查询:
topic = community.topics.order("last_active_at DESC").last
if topic && topic.last_active_at.to_date == Date.current.to_date
puts "posted today"
else
puts "no post today"
end
答案 2 :(得分:1)
您可以使用today?
方法:
def topic_button(community)
last_post = community.topics.order('last_active_at DESC').last
if last_post && last_post.last_active_at.today?
'posted today'
else
'no post today'
end
end