Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveAdmin custom filter with HABTM associations #1155

Closed
regedarek opened this issue Mar 20, 2012 · 20 comments
Closed

ActiveAdmin custom filter with HABTM associations #1155

regedarek opened this issue Mar 20, 2012 · 20 comments

Comments

@regedarek
Copy link

I'd like to be able to filter an object with this associations

ActiveAdmin.register Vendor do
  filter :"market" , :as => :select, :collection => Market.all
end

Model Market
  has_and_belongs_to_many :vendors
Model Vendor
  has_and_belongs_to_many :associated_markets, :class_name => "Market"

But I recieve an error:

undefined method market_eq' for #MetaSearch::Searches::Vendor:0x007f9f5c02dd18`

@latortuga
Copy link
Contributor

Did you try filter :associated_markets?

@regedarek
Copy link
Author

Solved

filter :associated_markets_id, collection: proc { Market.all }, as: :select

@treydock
Copy link

treydock commented Apr 4, 2012

I just ran into this and the solution above doesn't work in the exact same situation.

class User < ActiveRecord::Base
  has_and_belongs_to_many :organizations

class Organization < ActiveRecord::Base
  has_and_belongs_to_many  :users

Both suggestions fail the same

ActiveAdmin.register User do

  filter :organizations, :as => :select, :collection => Organization.all.collect {|o| [o.name, o.id]}


undefined method `organization_ids_eq' for #<MetaSearch::Searches::User:0x000000081dfa58>
ActiveAdmin.register User do
  filter :organizations_id, :as => :select, :collection => Organization.all.collect {|o| [o.name, o.id]}

undefined method `organization_ids_eq' for #<MetaSearch::Searches::User:0x00000007ef6170>

Right now the only way to make organizations searchable in the User resource is by searching by name.

ActiveAdmin.register User do
  filter :organizations_name, :as => :select, :collection => Organization.all.collect {|o| [o.name, o.name]}

@latortuga
Copy link
Contributor

@treydock can you try filter :organization? Trying to filter by multiple organization ids using a select field sounds doomed to fail :)

@treydock
Copy link

treydock commented Apr 6, 2012

@latortuga if I use only filter :organization the organization filter on the sidebar doesn't appear. My intention is to only allow filtering on a single organization, hence the :as => :select. For now the dropdown filter based on name works, but seems like using id should work just as well.

@latortuga
Copy link
Contributor

Maybe try filter :organization_ids_in. I apologize because I'm not super familiar with this part of AA but I know the filters use meta_search and that meta_search definitely supports searching through HABTM relationships so if that doesn't work, I encourage you to try some of the setups described in the meta_search documentation. Sorry again that I can't give you a more concrete answer.

@sturgill
Copy link

This doesn't appear to be a meta_search issue. If you manually force the parameters into the URL, it works as expected. Also, if you change the type to check_boxes then everything works as well.

This seems to be an issue with how the select box is being setup for rendering through Formtastic. Looking at ActiveAdmin::Inputs::FilterSelectInput, it always sets the name to end with _eq, meaning the select box disallows mutli-value queries (using an _in search).

I also don't see how we can set up a filter to use a mutli-select box with a defined height.

@Rio517
Copy link

Rio517 commented Oct 26, 2012

I also just bumped into this issue again, as well. Previously posted here: #1093

@carlos-a-crespo
Copy link

I'm facing the same problem. @sturgill is right, when changing the type to check_boxes then everything works as expected. Did anyone of you guys found a solution or workaround?

@sturgill
Copy link

I've done a little monkey patching on my fork of this project, but I haven't had time to do any extending testing (so I haven't submitted a pull request yet).

My work will allow you to specify a multiple select box, and set the size of it. It also changes the backend to use the _in selector for Meta Search instead of _eq. A really quick smoke test on my one specific use case passed, but I haven't looked beyond my own needs yet, so YMMV.

@chipairon
Copy link
Contributor

I have found a solution / workaround to this exception via a scope. I have put a simple demo project and the solution in the readme: https://github.com/Chipairon/active_admin_hbtm_association

class Post < ActiveRecord::Base
  attr_accessible :body, :title
  has_and_belongs_to_many :categories

  search_methods :category_ids_eq

  scope :category_ids_eq, lambda { |category_id|
    Post.joins(:categories).where("category_id = ?", category_id)
  }
end

@Rio517
Copy link

Rio517 commented Mar 11, 2013

Awesome. Thanks for sharing!

@lsmolic
Copy link

lsmolic commented Sep 8, 2013

Chipairon... you have rescued me from DAYS of continued pain. Thank you SOOO much.

@seanlinsley
Copy link
Contributor

@lsmolic as of #1928 Active Admin supports HABTM filters right out of the box

@luisinder
Copy link

If you want filter as "string" with ActiveAdmin options (eq, contains, starts_with, ends_with). you can do it like this:

in models/post.rb

    class Post < ActiveRecord::Base
      has_and_belongs_to_many :categories
      [:contains, :equals, :starts_with, :ends_with].each do |condition_type|
        search_methods "category_name_#{condition_type}"

        scope "category_name_#{condition_type}", lambda { |q|
          joins(:categories).merge Category.search("name_#{condition_type}" => q).relation
        }
      end
      # ...
    end

in admin/post.rb

    ActiveAdmin.register Post do
      # ...
      filter :category_name, as: :string
      # ...
    end

It works great for me. I hope you find it useful.

@NayanaBhagat
Copy link

What is search_methods in the above snippet?

@timoschilling
Copy link
Member

search_methods is a MetaSearch DSL method.

NOTE: The code snippeds are related to ActiveAdmin <= 0.6.x and are outdated for ActiveAdmin >= 1.0.0 / master

@NayanaBhagat
Copy link

Thanks for the update Tim... I found the same and in latest version its called ransacker instead of search_methods

So just replace search_methods by ransacker in your code while using the update version.

Thanks.

@EvanBurchard
Copy link

Thanks @NayanaBhagat @timoschilling and @luisinder. This worked for me.

@ipkes
Copy link

ipkes commented Jun 10, 2016

This works for me

class Post < ActiveRecord::Base
  has_and_belongs_to_many :categories

  SCOPES = [:contains, :equals, :starts_with, :ends_with]

  SCOPES.each do |condition_type|
    scope "category_name_#{condition_type}", lambda { |q|
      joins(:categories).merge Category.search("name_#{condition_type}" => q).result
    }
  end

  def self.ransackable_scopes(auth_object = nil)
    SCOPES.map{ |k| "category_name_#{k}" }
  end
end

@NayanaBhagat, @timoschilling, @luisinder, anyway thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests