1

How do I use greater than together with "or" condition in activerecord? Here is what I am trying to do:

@contract_dues_now = ContractDue.where(:status => ['Unpaid', 'Partially Paid']).
  where(due_date: Date.today || ContractDue.arel_table[:due_date].gt(Date.today)).
  group(:contract_id).order(:due_date) 

It doesnt seems correct to me as I this query result didnt output the OR statement like it suppose to:

enter image description here

Ryzal Yusoff
  • 957
  • 2
  • 22
  • 49

2 Answers2

1

This should do it:

@contract_dues_now = ContractDue.where(:status => ['Unpaid', 'Partially Paid']).
    where('contract_dues.due_date = ? OR contract_dues.due_date > ?', Date.today, Date.today).
    group(:contract_id).order(:due_date)
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
0

arel supports or operator.

where(ContractDue.arel_table[:due_date].eq(Date.today).or(ContractDue.arel_table[:due_date].gt(Date.today)))

https://github.com/rails/arel#more-sophisticated-queries

tyamagu2
  • 969
  • 7
  • 17