Olivier De Meulder Olivier De Meulder - 2 years ago 61
Ruby Question

where(:sku => 'sku123') vs where ('sku =

I have a strange situation. Here is my model:


=> UpcCode(id: integer, Upc: string, Sku: string, Active: boolean,
created_at: datetime, updated_at: datetime)

As you can see, Sku is one of my columns.

When I type in IRC:

UpcCode.where(:Sku => 'SKU123').first

I get expected result:

UpcCode Load (0.9ms) SELECT "upc_codes".* FROM "upc_codes" WHERE
"upc_codes"."Sku" = $1 ORDER BY "upc_codes"."id" ASC LIMIT 1
[["Sku", "GC-115-42"]] => #
<UpcCode id: 95, Upc: "70163098XXXX", Sku:
"SKU123", Active: true, created_at: "2016-05-11 03:12:11",
updated_at: "2016-05-11 03:12:11">

But when I try this using the following syntax:

UpcCode.where("Sku = 'SKU123'").first


UpcCode.where('Sku = "GC-115-42"').first

I am getting the following error as if the Sku column does not exist.

UpcCode Load (5.2ms) SELECT "upc_codes".* FROM "upc_codes" WHERE
(Sku = "GC-115-42") ORDER BY "upc_codes"."id" ASC LIMIT 1
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column
"sku" does not exist

I need to be able to use the second syntax because I need to manipulate the results. i.e. case insensitive comparison.

Answer Source

All identifiers that are not in double-quote are converted to lower case in PG therefor your column name Sku get converted to sku so it gives this issue you can try this way:

UpcCode.where('"Sku" = ?','SKU123' )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download