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

List tree items only with associations (+counts) #114

Closed
dmitry opened this issue Jul 26, 2014 · 14 comments
Closed

List tree items only with associations (+counts) #114

dmitry opened this issue Jul 26, 2014 · 14 comments

Comments

@dmitry
Copy link

dmitry commented Jul 26, 2014

Is there are some easy approach to get all the items of a tree as a nested hash (hash_tree) but only these that have any associations and return the number associated record to the item (with descendants)?

In my opinion it's a common task. How I'm doing it right now:

class Location < ActiveRecord::Base
  acts_as_tree

  has_many :places

  class << self
    def hash_tree_with_places(options = {})
      build_hash_tree(hash_tree_scope_with_places(options[:limit_depth]))
    end

    def hash_tree_scope_with_places(limit_depth = nil)
      select('COUNT(properties.id) AS properties_count, locations.*').
        joins(:descendant_hierarchies).
        joins('JOIN properties ON properties.location_id = location_hierarchies.descendant_id').
        from("(#{hash_tree_scope.to_sql}) locations").
        group('locations.id')
    end
  end
end

But I don't like it because:

  • Cannot add additional scope to the table scope (at least easily; current implementation, a little bit extended, uses option for this kind of problem).
  • This functionality should be embedded in the library.

Lets find the better solution and include it in the library.

@seuros
Copy link
Member

seuros commented Jul 26, 2014

This is a specific case, i don't think it good to add all the edge cases to the library.

@dmitry
Copy link
Author

dmitry commented Jul 27, 2014

Isn't it a common case to build the select (options) or navigation trees?

@seuros
Copy link
Member

seuros commented Jul 27, 2014

@dmitry
Copy link
Author

dmitry commented Jul 27, 2014

That's what I made actually. You can see it in the provided code above. But there are few drawbacks, that I've listed in the topic message.

@seuros
Copy link
Member

seuros commented Jul 27, 2014

And i said that the extra customization is application specific and not general case.
Anyway, you can write a PR with correct specs and send it. @mceachen will decide if that mergable or no.

@dmitry
Copy link
Author

dmitry commented Jul 27, 2014

I would like to discuss with someone who also would like to see the same functionality in the closure_tree core... because I don't know the best way how to implement it. I think to find out the best approach, we should discuss first, and then implement. So I would like to wait till someone else will respond on this "issue", please don't close it for now.

@mceachen
Copy link
Collaborator

Hi, I actually have to provide exactly this sort of metadata in my app as
well.

I found that when I was using mysql, the performance of the count() was
horribly bad with nontrivial numbers of labels (it might be ok with
postgres—I haven't tried it yet). I had to resort to a materialized value
of the count(
) in the db instead of doing the count(*) for every query.

If you went with this approach, you wouldn't have to pass a custom select
to the hash_tree method—just a custom where clause.

@AlexVPopov
Copy link
Contributor

👍 for this one. I also need this and currently I am using @dmitry's approach.

@AlexVPopov
Copy link
Contributor

Just to add: my idea is that if you have tag foo, which cannot be directly applied to, say photo or a product, but contains child tags bar and baz and both of these are applied to one product, than the products count of foo must be 2.

@mceachen
Copy link
Collaborator

Rather than have this functionality in the library, would a plugin make sense here?

In retrospect, all the deterministic ordering functionality, for example, was very invasive to this gem, and by having documented callback hooks, I may have been less complicated to have pulled it into a new sub-gem.

What RDBMS are you guys using?

@dmitry
Copy link
Author

dmitry commented Dec 10, 2014

MySQL here.

@dmitry
Copy link
Author

dmitry commented Dec 10, 2014

@seuros
Copy link
Member

seuros commented Dec 10, 2014

@dmitry Do you want to send a PR with a more flexible approach ?

@dmitry
Copy link
Author

dmitry commented Dec 10, 2014

@seuros not really, don't have a time right now, and I'm not required in such functionality anymore. Just a suggestion that it may be written in more flexible way (some parts of this big method may be extracted, as an example).

@seuros seuros closed this as completed Dec 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants