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

実際には、クエリを 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 に答える