Creating has_many association with ancestry gem

2019-06-09 22:30发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-06-09 22:49

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 ] } %>
查看更多
登录 后发表回答