Skip to content

Integrating with acts_as_list

sj26 edited this page Aug 1, 2012 · 5 revisions

If you want acts_as_list to work with ancestry, getting the sorting "sorted out" can be tricky.

The correct solution is hidden in one closed issue, but it's not trivial; here it is:

class Page < ActiveRecord::Base
  has_ancestry
  acts_as_list

  def scope_condition
    ancestry ? "ancestry = '#{ancestry}'" : 'ancestry IS NULL'
  end
end

Explanation: acts_as_list has a :scope option, but it can't be used on this case, since some logic is needed (roots are different from the rest of nodes, since their ancestry string is nil, not the empty string). Instead of using the :scope option, one can use the slightly more cumbersome scope_condition method, on which logic can be used.