Code Monkey home page Code Monkey logo

Comments (4)

rafaelfranca avatar rafaelfranca commented on August 27, 2024

Can you explain how that would be used and why?

from globalid.

rafacoello avatar rafacoello commented on August 27, 2024

Hello @rafaelfranca
ActiveRecord supports eager load (using includes method). This is useful in some cases that you know you will be calling a lot the relationships of a model, so you end having an N+1 queries issue.

E.g.

users = User.find([ 1, 2 ]) # This generates a query SELECT * FROM users WHERE id IN (1, 2)
users.first.posts # This generates a query SELECT * FROM posts WHERE user_id = 1
users.first.posts # This generates a query SELECT * FROM posts WHERE user_id = 1 again.

In the other hand:

users = User.includes(:posts).find([ 1, 2 ]) # This generates both queries (SELECT * FROM users WHERE id IN (1, 2)) and (SELECT * FROM posts WHERE user_id IN (1, 2))
users.first.posts # No query
users.last.posts # No query 

If we had this feature here, we could do something like GlobalID::Locator.locate_many([ user_gid_1, user_gid_2 ], {includes: :posts}) which would eager load the posts.

I know that can be done with a custom Locator (which I already did for my case) but I thought that having that option could be useful to anyone as it is very generic.

from globalid.

rafaelfranca avatar rafaelfranca commented on August 27, 2024

Makes sense. Do you want to open a PR for it?

from globalid.

rafacoello avatar rafacoello commented on August 27, 2024

Sure. Thanks! @rafaelfranca

from globalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.