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