سؤال

I'm using Solr to search for a list of companies. when I try to filter

Works

  companies = []
  current_user.cached_company.cached_companies.each do |company|
    companies << company.id
  end

Doesn't Work

  companies = []
  companies << current_user.cached_company.cached_companies.map(&:id)

When I call

  @search = Company.search do 
    with :id, companies
  end
  @companies = @search

It works with the first example but not the second.

However, this works just fine

  @search = Company.search do 
    with :id, current_user.cached_company.cached_companies.map(&:id)
  end
  @companies = @search

I know that I'm missing something simple here. I know it doesn't have to do with the caching, but cannot wrap my head around what's going on.

هل كانت مفيدة؟

المحلول

Your second example is putting a nested array in companies. Here's a simplified idea of what's going on:

data = [{value: 1}, {value: 2}, {value: 3}]

foo = []
data.each do |number|
  foo << number[:value]
end
p foo
# => [1,2,3] # One array with 3 values

foo = []
foo << data.map { |item| item[:value] }
p foo
# => [[1,2,3]] # One array with one value (another array with 3 values)

Either stick with your first version, or just do this:

companies = current_user.cached_company.cached_companies.map(&:id)

Or, if you want to stick with your 2nd version, make sure to flatten the values before you use them:

companies.flatten!
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top