我有几个模型想要按层次结构关联在一起。为了简单起见,假设我有这三个:
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 可以将另一个用户或组作为其父级。
有什么想法吗?
我过去这样做的方法是使用树中的多态容器,映射到特定的单独模型。
class Container < ActiveRecord::Base
acts_as_tree
belongs_to :containable, :polymorphic => true
end
class User
has_one :container :as => :containable
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)