简体   繁体   中英

How to fix StatementInvalid error with Stripe

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM