2

I want to make such a query for my Services ActiveRecord in Rails:

SELECT *
FROM "services" AS s
WHERE /* part using 's' alias */

Normally I'd write just Service.where(/* where part */), but I need to set my alias.

I tried to run ActiveRecord::Base.connection.execute(query), but result of that is not recognized as Service.

How can I handle it?

2 Answers 2

6

To use a table alias combine the select method with from:

Service.select("s.*").from("services s")

Which generates this SQL:

SELECT s.* FROM services s

And it returns an ActiveRecord::Relation which you can refine as needed.

Sign up to request clarification or add additional context in comments.

Comments

0

Fortunately I found solution during writing this question. Service.find_by_sql(query) works well in my case.

Comments

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.