April 16, 2024, 11:45 a.m. | Dmitry Daw

DEV Community dev.to

I need to get a selection of users with an SQL query like this:



sql =<<~SQL
token IS NULL OR
(
token = 'some_token'
AND (
state = 'cancelled' AND created_at > ?
OR state = 'submitted'
)
)
SQL
User.where(sql, Time.now)


I don't like to have SQL in my queries — all hidden code in scopes is now pops up, and it is hard to compose.


But could I rewrite this query with ActiveRecord? Let's try:



User
.where(token: nil) …

chatgpt lies null postgres queries query rails ruby sql sql query state token

Software Engineer for AI Training Data (School Specific)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Python)

@ G2i Inc | Remote

Software Engineer for AI Training Data (Tier 2)

@ G2i Inc | Remote

Data Engineer

@ Lemon.io | Remote: Europe, LATAM, Canada, UK, Asia, Oceania

Artificial Intelligence – Bioinformatic Expert

@ University of Texas Medical Branch | Galveston, TX

Lead Developer (AI)

@ Cere Network | San Francisco, US