stefankroes / ancestry

Organise ActiveRecord model into a tree structure
MIT License
3.72k stars 458 forks source link

Ancestry and pg_search and order scope #220

Open phlegx opened 9 years ago

phlegx commented 9 years ago

Hi!

Any idea how to use ancestry and pg_search (https://github.com/Casecommons/pg_search)? I want to search on all parents.

# app/models/zone.rb

class Zone < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search,
      against: :name, associated_against: {
          parent: :name,
      }
...

Search using Zone.all.search('My By Parent Name')

Another question: is it possible to order all zones by its parents name?

# app/models/zone.rb

class Zone < ActiveRecord::Base
...
    scope :order, -> { order('parents.name ASC') }
...
vanderhoorn commented 9 years ago

I'm not sure how pg_search works and/or combines with ancestry. But do note that ancestry's parent_id is not an Active Record column, but a method. Ancestry only uses (by default) the ancestry column.

phlegx commented 9 years ago

@vanderhoorn ok thank you. But how can I order all Zones by its parent name?

kbrock commented 5 years ago

I would like to introduce parent_id as a scope or actual sql. Will look into this but to be honest, probably won't happen any time soon