【问题标题】:acts_as_list inside acts_as_treeActs_as_tree 内的acts_as_list
【发布时间】:2015-10-05 12:53:55
【问题描述】:

假设我们有一个具有树状结构的List 模型(acts_as_tree)。因此,每个列表都可以有一个父级和/或子级。

基本上我希望列表的每个子级都在其父级范围内定义position 字段,如何实现?

class List < ActiveRecord::Base
  acts_as_tree
  # acts_as_list scope: :parent <-- desired result
end

或者,换句话说,acts_as_list gem 如何在List 模型中使用?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 rails-activerecord


    【解决方案1】:

    似乎将acts_as_list 范围限定为非关联:parent_id 字段(acts_as_tree gem 工作所必需的)完成了这项工作:

    class List < ActiveRecord::Base
      acts_as_tree
      acts_as_list scope: [:parent_id]
    end
    

    【讨论】:

    • 您甚至可以使用acts_as_tree order: :position 来使树排序。
    猜你喜欢
    • 2013-07-15
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多