Reputation: 1086
I want to exclude any records if they contain any part of my exclude
array. So an item can be 'Item name (BSE)' or 'Item name (B)' etc.
So far i have the following which does not work:
exclude = ['(BSE)', '(B)', '(SED)', '(DES)', '(VCS)']
@results = Results.where('results.item NOT IN (?)', exclude)
Is there a way to do this without listing each item in a LIKE statement or similar? My exclude array will grow over time so adding records to the array would be ideal rather then writing individual like statements.
Upvotes: 2
Views: 192
Reputation: 14890
This is just another possible solution, multiple chained where
clauses are joined with AND
by Rails.
exclude = %w[(BSE) (B) (SED) (DES) (VCS)]
exclude.each do |ex|
@results = Results.where('item NOT LIKE ?', ex)
end
Upvotes: 0
Reputation: 10227
How about this?
where = (1..exclude.count).to_a.fill('item NOT LIKE ?').join(' AND ')
@results = Results.where(where, *exclude.map {|x| "%#{x}%"})
Upvotes: 1