Creating has_many association with ancestry gem

倖福魔咒の 提交于 2019-12-23 04:26:56

问题


I installed ancestry gem & create Location Structure.

  • Alaska
  • California
    • Los Angeles
    • Fresno
    • Cincotta (Fresno)
    • Hammond (Fresno)
      • Melvin (Fresno)
        • Melvin 1
        • Melvin 2
        • Melvin 3
  • Arizona
  • Colorado

My post and location model

class Location < ActiveRecord::Base
 include Tree
 has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :location
end

When i am add new post, how to display only depth 4 level ( Melvin 1,Melvin 2,Melvin 3) as drop down.


回答1:


You have to enable cache depth so you can use at_depth:

Location.all.at_depth(4)

Thiscan be used to render a select input element:

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


来源:https://stackoverflow.com/questions/24787750/creating-has-many-association-with-ancestry-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!