ActiveRecord Find By Year, Day or Month on a Date field

后端 未结 14 1565
傲寒
傲寒 2020-11-27 10:00

I have an ActiveRecord model that has a date attribute. Is it possible to utilize that date attribute to find by Year, Day and Month:

Model.find_by_year(201         


        
14条回答
  •  悲&欢浪女
    2020-11-27 10:56

    In your Model:

    scope :by_year, lambda { |year| where('extract(year from created_at) = ?', year) }
    

    In your Controller:

    @courses = Course.by_year(params[:year])
    

提交回复
热议问题