Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info

    I have a scope like this : named_scope :open, :conditions => "brand is not null", and i would like to select all models that are subtree of this scope.
    Currently, i have to build a loop and selecting all subtrees of each model ; that is not efficient at all (n requests rather than 1).

    I found an other solution, that is not perfect since its not really chainable, and looks like this :

    named_scope :branded, lambda {{
    :joins => "LEFT JOIN groups AS ancestors ON (
    groups.ancestry = ancestors.id
    OR groups.ancestry LIKE CONCAT(ancestors.id,'/%')
    OR groups.ancestry LIKE CONCAT('%/', CONCAT(ancestors.id,'/%'))
    OR groups.ancestry LIKE CONCAT('%/',ancestors.id)
    OR ancestors.id = groups.id
    :conditions => '(ancestors.brand IS NOT NULL AND ancestors.brand !="")'

    I wished Ancestry provided real scopes that are chainables and did this job.
    Matt Hink
    So, quick question. How well does Ancestry play with the 'ltree' column type in Postgres?
    @mhink Late answer. AFAIK it doesn't. Ancestry utilizes a string column and stores paths like ":parent_id/:grand_parent_id". It constructs where queries in Arel that match forementioned string. In theory you could add very basic support for LTREE by using ltree2text when LTREE is used instead of a string column, but that would only slow performance. Adding full support for LTREE could be done as well, but would require some more work. Feel free to create a pull-request if you are up for it.
    Jimmy Baker
    Does anyone know how to property set a parent when using fixtures in rails 4?
    Matthew Brown
    Ancestry has been extremely useful since rails 1.2.6 for me. Is it still alive? I see we have glitter here, so that's a good sign....
    Matthew Brown
    Rishav Paul
    Hi, how can I order siblings in ancestry?
    Brandon Lawrence
    whats the syntax for adding an ancester to an object on creation?
    I tried Product.create(:name => 'Cheap Koozies', :parent => 'Drinkware') but it didn't work. The Drinkware object already exists

    @jimmybaker In rails 2, i do this :
    name: Grand parent

    name: Parent
    ancestry: '<%= Fixtures.identify(:grand_parent) %>'

    ancestry: '<%= Fixtures.identify(:grand_parent) %>/<%= Fixtures.identify(:parent) %>'

    Kyle Tully
    Hi All,
    Am using Ancestry inside a mountable engine on rails 4.2.2 and receive the following error "undefined local variable or method `has_ancestry' when adding "has_ancestry" to the model.rb file
    Is there anything different needed when installing Ancestry within a mountable engine?
    Oleksandr Bratashov

    Hello guys, could you help me with some question, when i setup ancestry for my model Category and create 4 items wich includes each other, last item has depth 2, but it shuld be 4 http://prntscr.com/br8hww

    A - 1
    B - 2
    C - 2 (should be 3)
    D - 2 (should be 4)

    Denny Mueller
    Hey anyone available for a second?
    Jason Frey
    what's up @dennym ?
    Tim Wisniewski
    Hi all šŸ‘‹ Iā€™d like to leverage recursive window queries (aka common table expressions, a modern feature of Postgres and MySQL) for my nesting/ancestry, to optimise query performance. Has anyone heard of a way to do that in rails?
    Jason Frey
    @timwis I haven't seen anything gemwise, personally, but there is this: https://sonnym.github.io/2017/06/05/common-table-expressions-in-activerecord-a-case-study-of-quantiles/
    and this - rails/rails#37944
    Tim Wisniewski
    @Fryguy thanks, I came across those as well. Given how complex those Arel bits look, it seems like a great candidate for a module :/
    Wish I knew enough about rails modules to build it myself šŸ¤”
    Tim Wisniewski
    Oh my gosh, I just realised that ancestry uses the same exact approach that I've been using in my app for 6 years! That means I can potentialy migrate to it without actually changing my database :O Only difference is my paths look like /1/3/5/ (with leading and trailing slashes)
    Julio Papel
    Hi guys, I added ancestry to my Category Model and it works nicely on the console, but to show a pull_down menu and update the parent of that category. I'm doing something like this: = fae_pulldown f, :parent, size: 'short', collection: Category.roots.order(:name) but it can't find the parent. if I use :ancestry it shows the form but cant submit! Any Ideas?
    Eric Pugh
    So... Any one have a recommendation for a front end tool for displaying a has_ancestry graph? Hoping to not start at the basics with D3 ;-)
    Eric Pugh
    how do I associate two existing objects togetehr? try_one.children << try_two was my first guess!
      try_two.parent = try_one
      assert_equal try_one.children.first, try_two
    Eric Pugh
    anyone done anything with DOT language and this gem?
    Jason Frey
    If you don't mind querying the whole thing up front, you can print it rather easily be using subtree.arrange, then walking it...
    def print_objs(subtree, indent = '')
      subtree = subtree.sort_by { |rec, _children| rec.name }
      subtree.each do |rec, children|
        puts "#{indent}- #{rec.name} (#{rec.id})"
        print_objs(children, "  #{indent}")
    roots = Model.roots.sort_by(&:name)
    roots.each do |root|
    Keenan Brock
    thanks jason. that looks slick