Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Root ordering #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AlexeyMatskevich wants to merge 2 commits into ClosureTree:master
base: master
Choose a base branch
Loading
from AlexeyMatskevich:root_ordering

Conversation

@AlexeyMatskevich
Copy link

@AlexeyMatskevich AlexeyMatskevich commented Oct 28, 2024
edited
Loading

Within the current implementation with root ordering there is a problem described in the readme

With numeric ordering, root nodes are, by default, assigned order values globally across the whole database table.

Also, changing the order of the root nodes is quite problematic since they are ordered globally as mentioned earlier.
Typical situation - I want to get all root blocks of a user and change their order:

block = Block.find(id)
old_order_block = block.siblings.where(user_id: user.id).find_by!(sort_order: sort_order)
old_order_block.prepend_sibling(block)

But in this case, the sort_order for all root nodes of one user will be a sparse sequence of 1, 10, 48 ....

I was thinking that in the case where parent_id = nil, you could rely on associations to restrict sort_order to root nodes within the association scope.

My option to implement this as simply as possible is to add an order_belong_to option, through which the foreign key field for the corresponding association is provided.

class Block < ApplicationRecord
 acts_as_tree numeric_order: true, order_belong_to: :user_id
end
class User < ApplicationRecord
end

derek-etherton-opslevel reacted with thumbs up emoji

We ran into the same issue. Our application uses multi-tenant, so it's critical that ordering is scoped per tenant instead of across the entire database.

This fix worked well for us, thanks @AlexeyMatskevich! Hope to see a future release with this included so that we don't need to maintain a fork.

AlexeyMatskevich reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /