【问题标题】:Acts as Tree with Multiple Models充当具有多个模型的树
【发布时间】:2011-01-09 16:21:01
【问题描述】:

我有几个模型,我想将它们分层关联在一起。为简单起见,假设我有这三个:

class Group < ActiveRecord::Base
  acts_as_tree
  has_many :users
end

class User < ActiveRecord::Base
  acts_as_tree
  belongs_to :group
  has_many :posts
end

class Post < ActiveRecord::Base
  acts_as_tree
  belongs_to :user
end

在当前的acts_as_tree 下,每个节点都可以单独与其他节点分层关联,只要它们属于同一类型。我想要删除对类型标识的这种限制,以便 SomePost.parent 可以将 User 或 Post 作为其父级,并且 SomeUser.parent 可以将另一个用户或组作为其父级。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-plugins acts-as-tree


    【解决方案1】:

    我过去这样做的方法是使用树中的多态容器,映射到特定的单个模型。

    class Container < ActiveRecord::Base
       acts_as_tree
       belongs_to :containable, :polymorphic => true 
    end
    
    class User
      has_one :container :as => :containable
    end
    

    【讨论】:

    • 这是否意味着帖子和群组也会有 has_one :container :as => :containable ?
    【解决方案2】:

    我设法做到了一点不同,但这可能不适用于您的情况。我正在重构现有代码,不想进行任何严重的数据库迁移。

    我想要单独的叶子和节点类。两者都继承自树类。

    我在vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb的ClassMethods中添加了两个函数:

        # Configuration options are:
        #
        # * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
        # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
        # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
        # * <tt>leaf_class_name</tt> - leaf class subtype of base tree class
        # * <tt>node_class_name</tt> - node class subtype of base tree class
        def acts_as_tree_node(options = {})
          configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil, :node_class_name => 'Node', :leaf_class_name => 'Leaf' }
          configuration.update(options) if options.is_a?(Hash)
    
          belongs_to :parent, :class_name => configuration[:node_class_name], :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
          #has_many :children, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
    
          class_eval <<-EOV
            has_many :child_nodes, :class_name => '#{configuration[:node_class_name]}', :foreign_key => "#{configuration[:foreign_key]}", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}, :dependent => :destroy
            has_many :child_leaves, :class_name => '#{configuration[:leaf_class_name]}', :foreign_key => "#{configuration[:foreign_key]}", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}}, :dependent => :destroy
    
            include ActiveRecord::Acts::Tree::InstanceMethods
    
            def self.roots
              find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
            end
    
            def self.root
              find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
            end
          EOV
        end
    
        # Configuration options are:
        #
        # * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
        # * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
        # * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
        # * <tt>node_class_name</tt> - the class name of the node (subclass of the tree base)
        def acts_as_tree_leaf(options = {})
          configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil, :node_class_name => 'Node' }
          configuration.update(options) if options.is_a?(Hash)
    
          belongs_to :parent, :class_name => configuration[:node_class_name], :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
    
          class_eval <<-EOV
            include ActiveRecord::Acts::Tree::InstanceMethods
    
            def self.roots
              find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
            end
    
            def self.root
              find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
            end
          EOV
        end
    

    然后,在 InstanceMethods 中,我只添加了一个函数:

        # Returns list of children, whether nodes or leaves.
        #
        # NOTE: Will not return both, because that would take two queries and
        # order will not be preserved.
        def children
          child_leaves.count == 0 ? child_nodes : child_leaves
        end
    

    这有点小技巧,但它对我有用,因为每个节点都有一种类型的潜艇。您可以使用children 函数来获得不同的行为,如下所示:

    def children
      child_nodes | child_leaves
    end
    

    但它仍然需要额外的查询,并且您会丢失订单和范围等内容。

    最后,在我的 Node 类中,我有

    acts_as_tree_node :node_class_name => 'NodeMatrix', :leaf_class_name => 'LeafMatrix'
    

    在我的 Leaf 课程中,如下:

    acts_as_tree_leaf :node_class_name => 'NodeMatrix'
    

    这两个都继承自 TreeMatrix,它是纯虚拟的(实际上没有任何东西被实例化为 TreeMatrix,它只是一个基类)。

    同样,这是非常特定于应用程序的。但它让您了解如何修改acts_as_tree。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 2017-01-14
      • 2016-09-06
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      相关资源
      最近更新 更多