0

I have simple query like

User.where('(zone_id IN (?) AND zone_type = "Org") OR (zone_id = ? AND zone_type ="Com")', [1,2,3], 10)

This throws me

PG::UndefinedColumn: ERROR:  column "Org" does not exist

What am i doing wrong?

Avdept
  • 2,261
  • 2
  • 26
  • 48
  • 1
    Not sure try this `User.where('(zone_id IN (?),zone_type = "Org") OR (zone_id = ?,zone_type ="Com")', [1,2,3], 10)` – Sonalkumar sute Mar 12 '15 at 11:44
  • `User.where('(zone_id IN (?),zone_type = ?) OR (zone_id = ?,zone_type =?)', [1,2,3], "org", 10, "com")` Try use this – Dipak Gupta Mar 12 '15 at 11:48
  • I don't think comas are valid to be used in "query" in `where`, check - http://guides.rubyonrails.org/active_record_querying.html#placeholder-conditions – Paweł Dawczak Mar 12 '15 at 11:50
  • Could you try: `User.where("(zone_id IN (:zone_ids) AND zone_type = :org_zone) OR (zone_id = :another_zone AND zone_type = :om_zone)", { zone_ids: [123], org_zone: "Org", another_zone: 10, com_zone: "Com" })`? – Paweł Dawczak Mar 12 '15 at 11:53
  • `User.where(zone_id: [1,3,5] AND zone_type:"org").or.where(zone_id: 10 AND zone_type: "com")` – Sonalkumar sute Mar 12 '15 at 11:54
  • @PawełDawczak, your suggestion was correct. I've replaced strings with ? and that worked. Thanks. Add this to answer and i accept it – Avdept Mar 12 '15 at 11:54

2 Answers2

1

More Rails way, You don't have to use IN operator

User.where(zone_id: [1,3,5] AND zone_type:"org").or.where(zone_id: 10 AND zone_type: "com")
Sonalkumar sute
  • 2,565
  • 2
  • 17
  • 27
1

Apparently replacing all conditions fixes the problem:

User.where('(zone_id IN (?) AND zone_type = "?") OR (zone_id = ? AND zone_type ="?")', [1,2,3], "Org", 10, "Com")

I would suggest following solition, which is more readable:

User.where(zone_id: [1,3,5] AND zone_type:"org").or.where(zone_id: 10 AND zone_type: "com")
Paweł Dawczak
  • 9,519
  • 2
  • 24
  • 37