我正在嘗試使用「if語句」,它將最近發佈的評論與今天的created_at
進行比較。爲什麼將created_at與當前日期進行比較會導致錯誤?
然而,這將返回錯誤:
應用助手
def chat_button(community)
if community.comments.last.created_at == Date.current.to_date
'Posted today'
else
'No post today'
end
end
它返回什麼錯誤? – sosborn 2013-04-24 23:42:52
@sosborn未定義的方法'created_at' – cat 2013-04-24 23:43:08
community.comments.last正在返回一個零對象(如果您還沒有保存任何註釋,則可能會返回),或者您尚未正確聲明所有關聯(community has_many comments) – sosborn 2013-04-24 23:46:45