Ryzal Yusoff
Ryzal Yusoff

Reputation: 1047

How to use greater than with "or" condition in Activerecord?

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

Upvotes: 1

Views: 775

Answers (2)

tyamagu2
tyamagu2

Reputation: 969

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

Upvotes: 0

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34336

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)

Upvotes: 1

Related Questions