Rails ActiveAdmin 自定义过滤器



我有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,在发票页面上,我希望能够根据给定的用户名进行过滤(用户通过报告拥有许多发票( 我已经浏览了文档,但我没有解决方案或开始如何做到这一点。所以问题是如何实现这个过滤器?

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

  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的查询,因此您将不得不使用更多的 adv.sql(join等(通过报告查找用户。

最新更新