search - Ruby difference between statements -


i'm using solr search list of companies. when try filter

works

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

doesn't work

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

when call

  @search = company.search      :id, companies   end   @companies = @search 

it works first example not second.

however, works fine

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

i know i'm missing simple here. know doesn't have caching, cannot wrap head around what's going on.

your second example putting nested array in companies. here's simplified idea of what's going on:

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

either stick first version, or this:

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

or, if want stick 2nd version, make sure flatten values before use them:

companies.flatten! 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -