0
我写了一个方法来检索在给定年份内创建的所有文章。更漂亮的ActiveRecord查询
def articles_for_year(year)
from = Date.strptime("#{year}-01-01").to_datetime
to = from.end_of_year
where(created_at: from..to)
en
此方法感觉有点笨重和低级别。它能以更好的方式实施吗?
对我来说,看起来并不笨重或低层次。 –
你应该看看这个[question](http://stackoverflow.com/questions/9624601/activerecord-find-by-year-day-or-month-on-a-date-field) – mdemolin
我认为你的方法是写,而@keymone已经给出了更多的'轨道'的方式:) – sameera207