Rails ActiveRecord - how to fetch records between two dates

后端 未结 8 1304
执念已碎
执念已碎 2020-12-29 23:16

I have in the database two date columns - from_date and to_date.

Example:

  • from_date: 2012-09-10
8条回答
  •  没有蜡笔的小新
    2020-12-29 23:53

    You could use below gem to find the records between dates,

    This gem quite easy to use and more clear By star am using this gem and the API more clear and documentation also well explained.

    ModelName.between_times(Time.zone.now - 3.hours,  # all posts in last 3 hours
                      Time.zone.now)
    

    Here you could pass our field also ModelName.by_month("January", field: :updated_at)

    Please see the documentation and try it.

提交回复
热议问题