我有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,并且在“发票”页面上,我希望能够基于给定的用户名进行过滤(用户通过报告拥有许多发票) 我已经浏览了文档,但是还没有解决方案或开始如何做。那么问题是如何实现此过滤器?
答案 0 :(得分:0)
也许我的答案是无关紧要的,因为我没有和活跃的管理员一起工作,但我会给你一个机会。
Report
是否包含belongs_to :user
?将一些参数传递给显示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
等)以通过报告查找用户。