在Ruby on Rails Admin上通过id将相同模型的其他记录关联起来

时间:2014-05-15 16:41:03

标签: ruby-on-rails ruby ruby-on-rails-3 rails-admin

我有一个名为Tags的模型,其中每个标记都有一个名称和描述,但标记可以是其他标记的父标记。

我想在我的模型中添加一个parent_id,但我想知道是否可以将Rails Admin配置为将子项与父项相关联(来自同一模型的另一条记录)。

谢谢:)

1 个答案:

答案 0 :(得分:0)

找到它:Creating a model that has a tree structure

感谢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