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

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

Asked on January 11, 2019 in Ruby on Rails.
Add Comment


  • 2 Answer(s)

    Rails3 command:

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

    Convert everything into the SQL notation:

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

    Answered on January 11, 2019.
    Add Comment

    Hit problems where column names are ambiguous: 

    date_field = Note.arel_table[:date]
    Note.where(user_id: current_user.id, notetype: p[:note_type]).
          where(date_field.gt(p[:date])).
          order(date_field.asc(), Note.arel_table[:created_at].asc())
    

    Answered on January 11, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.