Ruby stopping search at first array element

Viewed 50

I am attempting to use a drop down which will filter a table based on whether or not a premises contains a specific species. Some premises have multiple species. If I am attempting to find all premises with the species 'Horse' it will only find ones where horse is first in the array. If the specific element is not first in the array it is not filtered and shown. Here is the query used below.

@species = params[:species].presence
@default_species = @species.blank? ? 'All Species' : Species.find_cached_by_short_name(@species)&.long_name || @species

@prems = StatevetPrem.unmatched.search(@search, @page, @lines_per_page, { order: @sort })
@prems = @prems.where(state: @state) if @state

if @species
  prem_ids = StatevetDoc.
               unmatched.
               select(:holdings_match).
               where(default_species: @species).
               limit(@lines_per_page).
               collect { |doc| doc.holdings_match.values }.
               flatten.
               uniq[0,999]
  @prems = @prems.where(id: prem_ids)
end
1 Answers

Please use the select method instead of the collect method in your query.

Related