Skip to content
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

Calling hash_tree to get a subset of model data #325

Open
peterwake opened this issue Sep 24, 2018 · 1 comment
Open

Calling hash_tree to get a subset of model data #325

peterwake opened this issue Sep 24, 2018 · 1 comment

Comments

@peterwake
Copy link

With an example model DirectoryEntry, at the moment I can call directory_entry.hash_tree which runs the following query:

SELECT `directory_entries`.*
FROM `directory_entries`
INNER JOIN `directory_entry_hierarchies` ON `directory_entries`.`id` = `directory_entry_hierarchies`.`descendant_id`
WHERE `directory_entry_hierarchies`.`ancestor_id` = 2
ORDER BY `directory_entry_hierarchies`.generations ASC

What I would like to do is just draw a tree, and all I need for this is the id and name of the directory entries, (and probably parent_id to build the ). But I can't work out how to do get a select scope to work. Any suggestions? The query I'd expect to see would be something like:

SELECT 
  `directory_entries`.`id`,
  `directory_entries`.`name`,
  `directory_entries`.`parent_id`
FROM `directory_entries`
INNER JOIN `directory_entry_hierarchies` ON `directory_entries`.`id` = `directory_entry_hierarchies`.`descendant_id`
WHERE `directory_entry_hierarchies`.`ancestor_id` = 2
ORDER BY `directory_entry_hierarchies`.generations ASC

Thanks

@gerrywastaken
Copy link

For my use I only needed names for a test to make sure the correct hierarchy was being generated. All I personally needed was the following, so I'm providing it here in case others run into the same situation:

Tag.hash_tree.deep_transform_keys(&:to_s)

You could provide a block to deep_transform_keys if you wanted something else, however this won't help if you actually care about the SQL query generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants