Active Record - How to get all Direct and Indirect children in one query

tkz79

So i have a top level class for Topics, and each can have sub-topics through a parent_id

belongs_to :parent, :class_name => "Topic"
has_many :sub_topics, :class_name => "Topic", :foreign_key => "parent_id"

Each topic has channels...

has_many :channels

But this only gets the direct children.... and I've been struggling with a neat way to get all the channels that are decedents of a topic... including level 2, 3, 4, etc.....

has_many :sub_channels, :through => :sub_topics, source: :channels

This gets the second level channels, but ignores the top level and deeper levels... but this seems to be going down the wrong road for what I'm trying to attain...

Is there a good web resource that explains how to do this?

tkz79

Have spent quite a bit of time looking into this... seems the best way to go is to use one of the gems like nested set / ancestry to save having to build a lot of queries yourself. Thanks to the commenters above for the reco.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get parent with all children in one query

From Dev

How to get column from active record query

From Dev

get all children of a parent mysql one query for 5.6

From Dev

How to fetch an entity with all children associations in one query?

From Dev

How to retrieve a list of records and the count of each one's children with condition in Active Record?

From Dev

Active Record multiple selects in one query

From Dev

How to get all parents and children rows from one table in SQLite?

From Dev

Hibernate: Delete all children with one query

From Dev

Write a single rails active record query to get all the items by association with two tables?

From Dev

Codeigniter active record class - get all the fields from database except one

From Dev

How to get all children in firebase

From Dev

Trying to get direct, indirect, intersecting routes for public transport using an sql query

From Dev

sql query get all children and grandchildren

From Dev

How do you query Active record with enums

From Dev

How to write Subquery in codeigniter active record for this query

From Dev

How to write sub query in active record?

From Dev

How do i get all children (ie children of children) with Javascript?

From Dev

How to query for all direct subclasses in SPARQL?

From Dev

How to find root parent for all direct or indirect child in SQL Server table?

From Dev

How to get result of second table while using joins in rails active record query

From Dev

How to get result of second table while using joins in rails active record query

From Dev

How do I get Active Record query that works in Rails console working in a rake task?

From Dev

How to get all has_many associations from an active record collection

From Dev

How to get all has_many associations from an active record collection

From Dev

Active Record Query don't load all id's

From Dev

Get Direct Child - Not Nested Children

From Dev

How to recursively return the number of 'toy' items in the direct object and all of that object's direct children

From Dev

Complicated query with active record

From Dev

yii active record NOT IN query

Related Related

  1. 1

    How can I get parent with all children in one query

  2. 2

    How to get column from active record query

  3. 3

    get all children of a parent mysql one query for 5.6

  4. 4

    How to fetch an entity with all children associations in one query?

  5. 5

    How to retrieve a list of records and the count of each one's children with condition in Active Record?

  6. 6

    Active Record multiple selects in one query

  7. 7

    How to get all parents and children rows from one table in SQLite?

  8. 8

    Hibernate: Delete all children with one query

  9. 9

    Write a single rails active record query to get all the items by association with two tables?

  10. 10

    Codeigniter active record class - get all the fields from database except one

  11. 11

    How to get all children in firebase

  12. 12

    Trying to get direct, indirect, intersecting routes for public transport using an sql query

  13. 13

    sql query get all children and grandchildren

  14. 14

    How do you query Active record with enums

  15. 15

    How to write Subquery in codeigniter active record for this query

  16. 16

    How to write sub query in active record?

  17. 17

    How do i get all children (ie children of children) with Javascript?

  18. 18

    How to query for all direct subclasses in SPARQL?

  19. 19

    How to find root parent for all direct or indirect child in SQL Server table?

  20. 20

    How to get result of second table while using joins in rails active record query

  21. 21

    How to get result of second table while using joins in rails active record query

  22. 22

    How do I get Active Record query that works in Rails console working in a rake task?

  23. 23

    How to get all has_many associations from an active record collection

  24. 24

    How to get all has_many associations from an active record collection

  25. 25

    Active Record Query don't load all id's

  26. 26

    Get Direct Child - Not Nested Children

  27. 27

    How to recursively return the number of 'toy' items in the direct object and all of that object's direct children

  28. 28

    Complicated query with active record

  29. 29

    yii active record NOT IN query

HotTag

Archive