Rails where date is greater than given date query

后端 未结 3 1054
独厮守ぢ
独厮守ぢ 2021-02-03 17:04

Trying to search where movies coming out have a release date greater than today\'s date

 Movie.where(\'release > ?\', Date.today)
ActiveRecord::StatementInval         


        
相关标签:
3条回答
  • 2021-02-03 17:43

    Rails 3+ :

    Movie.where('release > ?', DateTime.now)
    

    Pre Rails 3

    Movie.where(['release > ?', DateTime.now])
    
    0 讨论(0)
  • 2021-02-03 17:44

    In recent versions of rails, you can do this:

    User.where(created_at: 3.days.ago..Time.now)
    

    See some other examples here: https://stackoverflow.com/a/24150094

    0 讨论(0)
  • 2021-02-03 17:46

    Update

    Rails core team decided to revert this change for a while, in order to discuss it in more detail. See this comment and this PR for more info.

    I am leaving my answer only for educational purposes.


    new 'syntax' for comparison in Rails 6.1 (Reverted)

    Movie.where('release >': DateTime.now)
    

    Here is a link to PR where you can find more examples.

    0 讨论(0)
提交回复
热议问题