Rails ActiveAdmin自定义过滤器

时间:2018-06-22 23:12:15

标签: ruby-on-rails ruby activeadmin ransack

我有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 个答案:

答案 0 :(得分:0)

也许我的答案是无关紧要的,因为我没有和活跃的管理员一起工作,但我会给你一个机会。

  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等)以通过报告查找用户。