Giter Club home page Giter Club logo

Comments (6)

GeorgeKaraszi avatar GeorgeKaraszi commented on July 26, 2024

this SELECT "groups".* FROM "groups" needs to turn into this SELECT "gtree".* FROM "gtree"

You're not calling the CTE you created.

from activerecordextended.

Merovex avatar Merovex commented on July 26, 2024

Okay. I'm looking at that now, though after a full day I'm a bit foggy. I think that query is being built in the
statement Group.union_all(group, recursive).to_union_sql

Or is it built here? Group.with.recursive(gtree: Group.union_all(base, recursive).to_union_sql)

I hard-coded the statement

   sql =<<EOS;
WITH RECURSIVE "gtree" AS ((
  (SELECT "groups".* FROM "groups" WHERE "groups"."id" = '7918c477-8843-471a-bfcd-e47982fed0c6')
  UNION ALL (SELECT "groups".* FROM "groups" JOIN gtree ON gtree.id = groups.parent_id)
  )) SELECT "gtree".* FROM "gtree"
EOS
      ActiveRecord::Base.connection.execute(sql)

The result is 7918c477-8843-471a-bfcd-e47982fed0c6 returns itself all its children. This is further than I was. I need to figure out how to get gtree into the ARExtended to get just this to work.

Then to work out how to get the heirarchy. I'm learning. But, this is going to be worth it since my old query was taking about 46ms and this takes 12ms (to get root, then its descendants).

from activerecordextended.

Merovex avatar Merovex commented on July 26, 2024

I've spent a couple hours trying to find how to get the following statement:

group = Group.where(id: root_id)
recursive = Group.joins('JOIN gtree ON gtree.id = groups.parent_id')
Group.with.recursive(gtree: Group.union_all(group, recursive).to_union_sql)

To create SELECT "gtree".* FROM "gtree" as the final query instead of SELECT "groups".* FROM "groups"

The closest I've gotten was:

group = Group.where(id: root_id)
recursive = Group.joins('JOIN gtree ON gtree.id = groups.parent_id')
Group.select('gtree.*').with.recursive(gtree: Group.union_all(group, recursive).to_union_sql)

which got me to SELECT "gtree".* FROM "groups"

I've read through your code and your tests. I don't see anywhere it's possible in ARExtended.

Edit: Okay, this is probably because the query should be in a relationship class instead. Going down that path, it doesn't like that there's a missing table. So, I'm heading into view country. But, I have this working as a raw query. Is there a better way?

I got it to work with the following hack:

group = Group.where(id: root_id)
recursive = Group.joins('JOIN gtree ON gtree.id = groups.parent_id')
sql = Group.select('gtree.*').with.recursive(
        gtree: Group.union_all(group, recursive).to_union_sql
      ).to_sql.sub(/FROM "groups"$/, 'FROM "gtree"')

return Group.find_by_sql(sql)

from activerecordextended.

GeorgeKaraszi avatar GeorgeKaraszi commented on July 26, 2024

You have 2 options.

Either:

Group.with.recursive(gtree: ...).select("*").from("gtree")

or

Group.with.recursive(gtree: ...).joins("INNER JOIN gtree ON gtree.id = groups.id")

from activerecordextended.

Merovex avatar Merovex commented on July 26, 2024

Wait...I have choices?! How dare you give me options. :-)

Here's what I have which appears to be working as advertized.

base = Group.where(id: root_id)
recursive = Group.joins('JOIN gtree ON gtree.id = groups.parent_id')
Group.with.recursive(
   gtree: Group.union_all(base, recursive).to_union_sql
).select("*").from("gtree")

Thank you for your patience. And, for letting me mind-dump in the comments of this question so others might benefit. I am very glad I was able to get ActiveRecord Extended to work. Keep up the good work.

from activerecordextended.

Merovex avatar Merovex commented on July 26, 2024

For what it's worth, the original query tracked around 36-42ms, this query was 0.9ms. That's a 4-5x improvement

from activerecordextended.

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.