- 
        Couldn't load subscription status. 
- Fork 246
A not_leaves scope
        Matt edited this page Apr 16, 2019 
        ·
        1 revision
      
    Tag.leaves exists, but apparently(?!) not Tag.not_leaves.  A little verbose and some brittle kludges, but maybe of interest.
  def self.not_leaves
    t = self.arel_table
    h = ::TaxonNameHierarchy.arel_table
    a = t.alias('a_')
    b = t.project(a[Arel.star]).from(a)
    c = h.alias('h1')
    b = b.join(c, Arel::Nodes::OuterJoin)
      .on(
        a[:id].eq(c[:ancestor_id])
    )
    
    e = c[:generations].not_eq(0)
    f = c[:ancestor_id].not_eq(c[:descendant_id])
    b = b.where(e.and(f))
    b = b.group(a[:id])
    b = b.as('tnh_')
     ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(t['id']))))
  end