0

我试图这样编码。
这有什么问题?

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
4

3 回答 3

1
 if  @community.topics.last_active_at == Date.current.to_date
        puts "posted today"
 else
        puts "no post today"
 end

希望能帮助到你

于 2013-04-25T00:19:10.253 回答
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
于 2013-04-25T00:21:37.360 回答
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
于 2013-04-25T00:21:49.410 回答