【问题标题】:Rails ActiveAdmin custom filterRails ActiveAdmin 自定义过滤器
【发布时间】:2018-06-22 23:12:14
【问题描述】:

我有 3 个模型:用户、报表和发票。

class User < ApplicationRecord
  has_many :reports , :dependent => :destroy
  has_many :invoices, :through => :reports

 class Report < ApplicationRecord
  has_many :invoices, :dependent => :destroy
  accepts_nested_attributes_for :invoices, allow_destroy: true

 class Invoice < ApplicationRecord
  belongs_to :report, optional: false
  validate :invoice_period

我已经开始使用 ActiveAdmin 并且在发票页面上我希望能够根据给定的用户名进行过滤(用户通过报告有许多发票) 我已经查看了文档,但我没有解决方案或开始如何执行此操作。那么问题是如何实现这个过滤器?

【问题讨论】:

    标签: ruby-on-rails ruby activeadmin ransack


    【解决方案1】:

    也许我的回答无关紧要,因为我没有与活跃的管理员合作,但我会试一试。

    1. Report 不应该包含belongs_to :user
    2. 将一些参数传递给显示Invoice page 并显示基于User 的发票的控制器的操作。

      @invoices = User.where(:name => params[:user_name]).invoices if params[:user_name].present ?
      

    如果您有其他参数,您可以使用范围来加入查询。本文将帮助https://www.justinweiss.com/articles/search-and-filter-rails-models-without-bloating-your-controller/

    可能在您的show invoice controller 中有基于Invoice 的查询,因此您将不得不使用更多的广告。 sql (join 等) 通过报告查找用户。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多