1

I'm currently using ancestry gem to do a hierarchy list of my "posts", but I need to have the option to a child have multiple parents, and with ancestry I can't do that. You guys have any solution? Adapting ancestry with other gem or using other gem instead. Thanks

terrorista
  • 227
  • 1
  • 15

1 Answers1

0

Use HABATM association

What you want to achieve is essentially has_and_belongs_to_many relationship. You can create a relationship on yourself and that should do the trick. If you need help on how to do this with HABTM association, here is a SO answer that shows how.

class Post < ActiveRecord::Base
  has_and_belongs_to_many :children, 
              class_name: "Post", 
              join_table: :children, 
              foreign_key: :post_id, 
              association_foreign_key: :child_post_id
end

Alternatively, use a gem

You can use acts-as-taggable-on gem to tag your posts. It would be better if you create a model or enum that holds your tags, so that you don't mistype them while tagging.

You can query your posts like this:

Post.tagged_with(["ruby", "rails"], :any => true)
Community
  • 1
  • 1
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54