3

My below active record statements are working fine:

Requirement.where(no: 0)

Requirement.yes_no.or(Requirement.where.not(parent_req: nil))

In Requirement model I have an enum field, which is working fine too:

enum response_type: { yes_no: 0, document: 1, interviewee: 2, sample_set: 3, brief_desc: 4 }

But when I combine above two statements:

Requirement.where(no: 0).where(Requirement.yes_no.or(Requirement.where.not(parent_req: nil)))

It is giving me error:

ArgumentError: Unsupported argument type: #<Requirement::ActiveRecord_Relation:0x005567c7c9bf88> (Requirement::ActiveRecord_Relation)

I expect the following query to be run:

SELECT `requirements`.*
  FROM `requirements`
 WHERE `requirements`.`no` = 0
   AND (`requirements`.`response_type` = 0 
    OR (`requirements`.`parent_req` IS NOT NULL))
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rajkaran Mishra
  • 4,532
  • 2
  • 36
  • 61

2 Answers2

1

Rails 5: ActiveRecord OR query

Requirement.yes_no.or(Requirement.where.not(parent_req: nil)).where(no: 0)

put where(no: 0) at the end of ar query

antonovga
  • 43
  • 4
1

To combine using AND (intersection), merge as mentioned by Tom Lord worked.

Requirement.where(no: 0).merge(Requirement.yes_no.or(Requirement.where.not(parent_req: nil)))
Rajkaran Mishra
  • 4,532
  • 2
  • 36
  • 61