[英]Relate record with other record of same model by id on Ruby on Rails Admin
I have a model named Tags where each tag has a name and description but a tag can be the parent for other tags. 我有一个名为标签的模型,其中每个标签都有名称和描述,但是标签可以是其他标签的父对象。
I was thinking of adding a parent_id to my model but I want to know if Rails Admin can be configured to relate the child to a parent (another record from same model). 我当时想在模型中添加一个parent_id,但是我想知道是否可以配置Rails Admin将子代与父代关联(同一模型的另一个记录)。
Thanks :) 谢谢 :)
Found it on: Creating a model that has a tree structure 发现于: 创建具有树结构的模型
Thanks to coreyward :) 感谢coreyward :)
class Tag < ActiveRecord::Base
attr_accessible :description, :name, :parent_id
has_many :children, class_name: "Tag", foreign_key: "parent_id"
belongs_to :parent, class_name: "Tag",primary_key: "id"
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.