[英]Filter on parent object attribute in ActiveAdmin
我希望能夠根據對象父級的屬性過濾對象:
class Call < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :calls
end
我希望能夠這樣做:
ActiveAdmin.register Call do
filter :user
end
並讓它過濾user.name,而不是呈現所有用戶的選擇。 可以這樣做嗎?
丹尼斯的解決方案幾乎對我有用。 我只需要添加過濾器類型。 例如:
ActiveAdmin.register Call do
filter :user_name, :as => :string
end
嘗試這個:
ActiveAdmin.register Call do
filter :user_name
end
由於ActiveAdmin使用meta_search
過濾器,因此他們的doc非常有用: https : //github.com/ernie/meta_search
您可以使用ActiveAdmin使用的InheritedResource中的嵌套資源,因此父級會自動篩選您的列表。
ActiveAdmin.register User do
# this is the parent resource
end
ActiveAdmin.register Call do
belongs_to :user # nested below resource user
end
然后,您可以使用rake路由查看由ActiveAdmin生成的新嵌套路由:)希望它有所幫助
在ActiveAdmin的下一個版本中(我使用1.0.0.pre),您可以使用Ransack方法。 所以,假設你有一篇文章,屬於用戶。
您將擁有以下admin / article.rb文件
ActiveAdmin.register Article do
controller do
def scoped_collection
Article.includes(:user)
end
end
index do
column :id
column :created_at
column :title
column("Author", sortable: 'users.first_name') { |item| link_to item.user.full_name, user_path(item.user) }
actions
end
filter :user_first_name_cont, :as => :string
filter :user_last_name_cont, :as => :string
end
這里,user_first_name_cont是ransack方法,它過濾關聯的用戶first_name,'cont'表示包含。
我會說這在很大程度上取決於你的模型之間的關聯類型 - 我需要花費數小時來解決這個問題。
例
class User < ActiveRecord::Base
belongs_to :user
end
class Email < ActiveRecord::Base
has_one :email
end
要根據用戶的電子郵件過濾用戶,請執行此操作(不要忘記as: :string
部分,因為它可以讓您訪問Ransack搜索方法,例如包含等等)
ActiveAdmin.register User do
filter :user_email, :as => :string
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.