ruby-on-railsrubyruby-on-rails-4acts-as-taggable-onacts-as-tree

Acts as Tree gem - Is there any way to make an element have two parents?


I am using the acts_as_tree gem in order to implement a category list in which a freelancer can select categories and subcategories. "Web design" is one of the subcategories, and it should have two parents: "Design" and "Web development". Is there any way to achieve two parents with the acts_as_Tree gem?

For example:

web_development.children.create({:label => 'Web Design'})
design.children.create({:label => 'Web Design'})

The code above is close to what I am trying to do, except it will make two categories called "web design". I only want one, but I want it to have two parents.


Solution

  • If a node can have two parents, than the structure is not a tree, is it?

    The answer is no - you can't have two parents for one category, since category's parent is defined as a foreign-key on the parent column in a one-to-many relationship. There is no way to assign two parents unless you change the underlying structure:

    belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]