PostgreSQL客户查询SQL的Ruby on Rails等效完整语句问询
Complete Rails Equivalent Query for Your PostgreSQL Statement
Got it, let's fix and finish that Rails query to match your original PostgreSQL logic. Your SQL checks if a customer's first_name, last_name, email, or main_phone is present in your target array—here's the correct, fully functional Rails version:
array = ["John","Doe","111-111-1111"] Customer.where(first_name: array) .or(Customer.where(last_name: array)) .or(Customer.where(email: array)) .or(Customer.where(main_phone: array))
Quick breakdown:
- Each
.orcall chains a new condition from the sameCustomermodel (fixing the typos likecustomers.whereor incompletecustome...in your partial code) - This generates SQL that's logically identical to your original statement—all four fields are checked against the array, with OR logic connecting each condition
- Rails will handle proper quoting and PostgreSQL-compatible syntax automatically, so you don't have to manually replicate the quoted column names from your raw SQL
内容的提问来源于stack exchange,提问作者Cannon Moyer
相关产品推荐
相关产品推荐

