[英]Rails ActiveAdmin custom filter
我有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,並且在“發票”頁面上我希望能夠基於給定的用戶名進行篩選(用戶通過報告擁有許多發票)我已經瀏覽了文檔,但是我沒有解決方案或開始如何做這個。 那么問題是如何實現此過濾器?
也許我的答案是無關緊要的,因為我沒有和活躍的管理員一起工作,但我會嘗試一下。
Report
包含belongs_to :user
? 將一些參數傳遞給顯示Invoice page
並基於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
等)以通過報告查找用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.