今天想写个两级分类,打算用一个表做。
结果把model定义来定义去,发现正好写了一个类似act_as_tree一句话就搞定的东西。
我的定义:
# 自引用的连接
belongs_to :parent,
:class_name => "NewsCategory",
:foreign_key => "parent_id" # 这句可以省略
has_many :children,
:class_name => "NewsCategory",
:foreign_key => "parent_id",
:order => :position,
:dependent => :destroy
acts_as_list :scope => :parent
用act_as_tree实现:
acts_as_list :scope => :parent_id # 确保position字段有效
acts_as_tree :order => :position
哈哈,ror真不错,已经产生了非常大的学习和使用的快感!!