1

I need to pull aggregated columns from my database in a way that's unique to this page, crosses several models, and is expensive to aggregate in memory on a page that already has performance concerns. As a result, I want to make a hand-written SQL query to the database and return a bunch of simple objects (like hashes, or structs) which I can then dole out to the existing objects that consume this information. ActiveRecord::Base.connection.select_all is perfect for what I want.

My SQL string takes an array of ids:

WHERE entry.task_id IN (#{@project.tasks.id})

but this doesn't work because it returns a wrapped array, and connection.quote returns a bulleted list for some reason. What would be the best way to get the information I want? Should I manually strip off the [ and ]? Is there a handy, accessible function like sanitize_sql_array that'll do the trick? Should I never be calling select_all as part of normal operation?

This appears to have been easier in the past, but most of the methods I would use have been protected (including sanitize_sql_array). Rails really wants me to use ActiveRecord query methods, it seems.

1 Answer 1

3

Join and make your array a string, then pass it to the SQL:

ids_string = ids.join(', ')
Sign up to request clarification or add additional context in comments.

1 Comment

Was that simple. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.