Can you do greater than comparison on a date in a Rails 3 search?

后端 未结 4 397
天命终不由人
天命终不由人 2020-12-04 08:52

I have this search in Rails 3:

Note.where(:user_id => current_user.id, :notetype => p[:note_type], :date => p[:date]).order(\'date ASC, created_at A         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 09:48

    Rails 6.1 added a new 'syntax' for comparison operators in where conditions, for example:

    Post.where('id >': 9)
    Post.where('id >=': 9)
    Post.where('id <': 3)
    Post.where('id <=': 3)
    

    So your query can be rewritten as follows:

    Note
      .where(user_id: current_user.id, notetype: p[:note_type], 'date >', p[:date])
      .order(date: :asc, created_at: :asc)
    

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

提交回复
热议问题