• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: What is the Ruby ActiveRecord equivalent for this SQL?

Resolved: What is the Ruby ActiveRecord equivalent for this SQL?

0
By Isaac Tonny on 04/04/2023 Issue
Share
Facebook Twitter LinkedIn

In this post, we will see how to resolve What is the Ruby ActiveRecord equivalent for this SQL?

Question:

What would be an optimized (fast) ActiveRecord equivalent in Ruby:
Note: shop_id is a ruby variable passed to the query/association. Thank you
This above SQL runs as fast as expected.
I am trying to find a pure ActiveRecord solution to avoid something like this:
The reason is to avoid having hardcoded SQL in the ActiveRecord to prevent possible future issues with backward compatibility of eventual model chages and migrations…

Best Answer:

I think you do not need the JOIN at all. A subquery in the where should be enough:
Note that the customer_ids is only a ActiveRecord::Relation and that the query is not run immediately. It is only run as subquery when the second query is executed.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

optimization ruby ruby-on-rails sql
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to stop a thread that has a blocking function from easygui in python

05/04/2023

Resolved: Removing null keys from a json array of objects

05/04/2023

Resolved: How can I generate at compile-time a separate OpenAPI Swagger.json file for each Controller in my ASP.NET project?

05/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.