Search code examples
ruby-on-railsrubypostgresqlpg

PG error using rails where


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?


Solution

  • 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")