【问题标题】:Rails Admin - List, Scope on associationRails 管理员 - 列表、关联范围
【发布时间】:2015-11-30 11:53:54
【问题描述】:

我有分类和文章。他们有一个HABTM关系。我希望可以在文章列表视图中列出每个类别以及与该文章相关的文章。

我怎样才能做到这一点?

我尝试过使用范围,但这需要一个函数?

https://github.com/mathieul/rails_admin

【问题讨论】:

    标签: ruby-on-rails scope rails-admin


    【解决方案1】:

    我会创建一个自定义方法,然后包含在字段中

    class Article
      include Mongoid::Document
      field :title, type: String
      field :body, type: String
      has_and_belongs_to_many :categories
    
      def category_articles
        output = []
        categories.each do |cat|
          output << cat.articles.pluck(:title)
        end
        return output.flatten.join(', ')
      end
    
      rails_admin do
        list do
          include_all_fields
          field :category_articles
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多