Laetitia

Laetitia

How to model nested categories?

Hi everybody,
I want to implement a system with categories and subcategories and subsub and subsubsub… lol.

ie: the category A has 2 subcategories: B and C and C have a sub-category D.

The deep of the nesting is not rigid and may grow.
How would you model this, allowing easy search in db? Search ancestor and children? Is there a way to avoid recursion?

Most Liked

al2o3cr

al2o3cr

Postgres can do recursive common-table expressions if you really want, but it’s worth considering carefully exactly how your application will use the nested categories.

  • how often are new categories added? Some representations will make this expensive
  • how often do queries like “in this category and all its subcategories” occur? Some representations will make THAT expensive

Poking around in Hex turns up some candidates:

  • Arbor uses CTEs to traverse parent_id chains
  • Ancestry uses materialized paths; each record stores the full “path” through the hierarchy (a record in Subsubcategory D would store “A/C/D” or similar)
  • as_nested_set uses nested sets to make answering “and all subcatgories” questions faster
  • Hierarch linked up-thread by @fuelen uses a Postgres extension to offload some of the complexity to the database
fuelen

fuelen

Hello

Did you see this library GitHub - Byzanteam-Labs/hierarch: Tree structure & hierarchy for ecto models with ltree(Postgres) · GitHub?
It is based on ltree postgres extension. I didn’t try it, but I’d try if I have this kind of requirements :slight_smile:

kokolegorille

kokolegorille

Another way is to use a nested set model, using lft and rgt additional columns.

https://www.sciencedirect.com/topics/computer-science/nested-set-model

Last Post!

dimitarvp

dimitarvp

These are the famous last words written on the tombstones of countless never-released projects though… :icon_cry:

It will never be perfect. Post it on GitHub when you get a free hour some evening. People like me might like it and offer PRs.

Don’t be obsessed with a theoretical perfect state of your code. I know exactly how you feel but we the techies should apply some business thinking to our own voluntary work here and there: if it works and it’s good enough then it’s very likely just fine for presentation as well.

You don’t want to know how god-awful my Rust code in my sqlite3 Elixir<=>Rust library is. But it does what it’s supposed to for the moment. Overcoming analysis paralysis and the desire for perfection are skills that will put you above many programmers.

At least from my side I can promise not to judge. Only to send PRs if I feel that something can be improved.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement