scope :with_any_category_ids, ->(category_ids) {
# get a reference to the join table
company_categories = CompanyCategory.arel_table
# get a reference to the filtered table
companies = Company.arel_table
# let AREL generate a complex SQL query
where(
CompanyCategory
.where(company_categories[:company_id].eq(companies[:id]))
.where(company_categories[:category_id].in([*category_ids].map(&:to_i)))
.exists,
)
}
Hi @jhund
ruby 3.1.2 rails 7.0.1 filterrific (5.2.2)
I'm trying to filter companies with the selected category. I have the following many-to-many relationship between Company and Category.
company.rb
has_many :company_categories has_many :categories, through: :company_categories
category.rb
has_many :company_categories has_many :companies, through: :company_categories
company_category.rb
companies/index.html.erb
<%= f.select :with_any_category_ids, @filterrific.select_options[:with_any_category_ids], { :include_blank => 'categories' }, { :class => 'form-control' } %>
companies_controllers.rb
I keep getting _undefined method `exists' whenever I try to filter companies with the selected category
I have no problem with other scopes: Filter by column values, Filter by ranges etc. except this one (Filter by existence of many-to-many association)
And I follow everything here http://filterrific.clearcove.ca/pages/active_record_scope_patterns.html to the T.