与祖先gem创建has_many关联

我安装了祖先gem并创建了Location Structure。

  • 阿拉斯加州
  • 加州
    • 洛杉矶
    • 弗雷斯诺
    • Cincotta(弗雷斯诺)
    • 哈蒙德(弗雷斯诺)
      • 梅尔文(弗雷斯诺)
        • 梅尔文1
        • 梅尔文2
        • 梅尔文3
  • 亚利桑那
  • 科罗拉多州

我的post和位置模型

class Location < ActiveRecord::Base include Tree has_many :posts end class Post < ActiveRecord::Base belongs_to :location end 

当我添加新post时,如何仅显示深度4级( Melvin 1,Melvin 2,Melvin 3 )作为下拉。

您必须启用缓存深度,以便使用at_depth :

 Location.all.at_depth(4) 

这可以用于渲染select输入元素:

 <%= select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %>