-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Comments
Did you try |
Solved
|
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]} |
@treydock can you try |
@latortuga if I use only |
Maybe try |
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 I also don't see how we can set up a filter to use a mutli-select box with a defined height. |
I also just bumped into this issue again, as well. Previously posted here: #1093 |
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? |
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 |
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 |
Awesome. Thanks for sharing! |
Chipairon... you have rescued me from DAYS of continued pain. Thank you SOOO much. |
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. |
What is search_methods in the above snippet? |
NOTE: The code snippeds are related to ActiveAdmin |
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. |
Thanks @NayanaBhagat @timoschilling and @luisinder. This worked for me. |
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! |
I'd like to be able to filter an object with this associations
But I recieve an error:
undefined method
market_eq' for #MetaSearch::Searches::Vendor:0x007f9f5c02dd18`The text was updated successfully, but these errors were encountered: