0
votes

I'm working on payment system with Stripe. I have to search customer after receiving stripe webhook. But this error below occurs.


ActiveRecord::StatementInvalid (SQLite3::SQLException: near "'cus_xxxxx'": syntax error: SELECT "users".* FROM "users" WHERE (stripe_customer_id 'cus_xxxxx') ORDER BY "users"."id" ASC LIMIT ?):


Customer which has cus_xxxxx exists. But I have to remove single quotation from this...

@user = User.where('stripe_customer_id ?', "#{source['customer'].gsub("'", "")}").first

I have tried like this. But that wouldn't work.

1

1 Answers

1
votes

Your problem is that you aren’t using an operator in the query. Ex:stripe_customer_id = ? or stripe_customer_id IN (?). But there’s a better way to write this.

@user = User.find_by({
  stripe_customer_id: source['customer'].delete("'")
})

find_by takes similar arguments to where, but returns the first record. Also, the Hash syntax automatically chooses between =, IN, or IS NULL depending on the value provided.

Also, you can use the delete method to remove all occurrences of a string within a string. Benchmarks show that it’s faster than gsub. It is odd to me that you need to modify the string like this though. I feel like whatever is submitting source is doing it wrong if there’s extra quotes.