使用祖先gem创建has_many关联 [英] Creating has_many association with ancestry gem

查看:86
本文介绍了使用祖先gem创建has_many关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了祖传宝石&创建位置结构。

I installed ancestry gem & create Location Structure.


  • 阿拉斯加

  • 加利福尼亚州

    • 洛杉矶

    • 弗雷斯诺

    • 辛科塔(弗雷斯诺)

    • 哈蒙德(弗雷斯诺)

      • 梅尔文(弗雷斯诺)

        • 梅尔文1

        • 梅尔文2

        • Melvin 3

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

              我的信息和位置模型

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

              当我添加新内容时帖子中,如何只显示深度4级别( Melvin 1,Melvin 2,Melvin 3 )作为下拉列表。

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

              推荐答案

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

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

              Location.all.at_depth(4)
              

              这可以用来呈现 select 输入元素:

              Thiscan be used to render a select input element:

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

              这篇关于使用祖先gem创建has_many关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆