在ActiveAdmin中筛选父对象属性

时间:2022-07-19 15:58:56

I'd like to be able to filter an object based on an attribute of it's parent:

我希望能够根据对象父级的属性过滤对象:

class Call < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :calls
end

I'd like to be able to do this:

我希望能够这样做:

ActiveAdmin.register Call do
  filter :user
end

and have it filter on user.name, rather than present a select of all users. Can this be done?

并让它过滤user.name,而不是呈现所有用户的选择。可以这样做吗?

4 个解决方案

#1


29  

Denis's solution almost worked for me. I just needed to add the filter type. For example:

丹尼斯的解决方案几乎对我有用。我只需要添加过滤器类型。例如:

ActiveAdmin.register Call do
  filter :user_name, :as => :string
end

#2


8  

Try this:

尝试这个:

ActiveAdmin.register Call do
  filter :user_name
end

Since ActiveAdmin uses meta_search for filters their doc is very helpful: https://github.com/ernie/meta_search

由于ActiveAdmin使用meta_search过滤器,因此他们的doc非常有用:https://github.com/ernie/meta_search

#3


3  

You can use nested resources from InheritedResource which is used by ActiveAdmin, so your list is automatically filtered by the parent.

您可以使用ActiveAdmin使用的InheritedResource中的嵌套资源,因此父级会自动筛选您的列表。

ActiveAdmin.register User do
  # this is the parent resource
end

ActiveAdmin.register Call do
  belongs_to :user # nested below resource user
end

You can then use rake routes to see the new nested routes, generated by ActiveAdmin :) Hope it helps

然后,您可以使用rake路由查看由ActiveAdmin生成的新嵌套路由:)希望它有所帮助

#4


2  

In the next release of ActiveAdmin (I work with 1.0.0.pre) you can use Ransack methods. So, let say you have an Article, which belongs_to User.

在ActiveAdmin的下一个版本(我使用1.0.0.pre)中,您可以使用Ransack方法。所以,假设你有一篇文章,属于用户。

You will have the following admin/article.rb file

您将拥有以下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

Here, user_first_name_cont is ransack method which filters on associated user first_name and 'cont' means contains.

这里,user_first_name_cont是ransack方法,它过滤关联的用户first_name,'cont'表示包含。

#1


29  

Denis's solution almost worked for me. I just needed to add the filter type. For example:

丹尼斯的解决方案几乎对我有用。我只需要添加过滤器类型。例如:

ActiveAdmin.register Call do
  filter :user_name, :as => :string
end

#2


8  

Try this:

尝试这个:

ActiveAdmin.register Call do
  filter :user_name
end

Since ActiveAdmin uses meta_search for filters their doc is very helpful: https://github.com/ernie/meta_search

由于ActiveAdmin使用meta_search过滤器,因此他们的doc非常有用:https://github.com/ernie/meta_search

#3


3  

You can use nested resources from InheritedResource which is used by ActiveAdmin, so your list is automatically filtered by the parent.

您可以使用ActiveAdmin使用的InheritedResource中的嵌套资源,因此父级会自动筛选您的列表。

ActiveAdmin.register User do
  # this is the parent resource
end

ActiveAdmin.register Call do
  belongs_to :user # nested below resource user
end

You can then use rake routes to see the new nested routes, generated by ActiveAdmin :) Hope it helps

然后,您可以使用rake路由查看由ActiveAdmin生成的新嵌套路由:)希望它有所帮助

#4


2  

In the next release of ActiveAdmin (I work with 1.0.0.pre) you can use Ransack methods. So, let say you have an Article, which belongs_to User.

在ActiveAdmin的下一个版本(我使用1.0.0.pre)中,您可以使用Ransack方法。所以,假设你有一篇文章,属于用户。

You will have the following admin/article.rb file

您将拥有以下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

Here, user_first_name_cont is ransack method which filters on associated user first_name and 'cont' means contains.

这里,user_first_name_cont是ransack方法,它过滤关联的用户first_name,'cont'表示包含。