Cant deal with array filtering. This:
@profiles = Profile.filter(params.slice(:location, :status, :items))
Passes records to be filtered:
def filter(filtering_params)
results = self.where(nil)
puts filtering_params
filtering_params.each do |key, value|
if value.is_a?(Array)
results = results.public_send(key, value.to_s) if self.where("#{key}", value)
else
results = results.public_send(key, value) if value.present?
end
end
results
end
But there are some problems with Postgres Array. Params passed to filter method are:
Parameters: {"utf8"=>"✓", "location"=>"0", "status"=>"0", "items"=>["0", "1"], "commit"=>"Filtruoti"}
and method receives them:
{"location"=>"0", "status"=>"0", "items"=>["0", "1"]}
But I get error:
PG::InvalidTextRepresentation: ERROR: malformed array literal: "0" LINE 1: ..." = $2 AND "profiles"."items" IN ('0', '1') ... ^
DETAIL: Array value must start with "{" or dimension information. : SELECT "profiles".* FROM "profiles" WHERE "profiles"."location" = $1 AND "profiles"."status" = $2 AND "profiles"."items" IN ('0', '1') LIMIT $3 OFFSET $4
And this error actually points to where I want to display received records:
<% rofiles.each do |profile| %>
What is happening with this Postgres Array..?
gem 'rails', '~> 5.0.3'
gem 'pg', '~> 0.18'
EDIT:
Looks like t works well now with scope:
scope :items, -> (items) { where "items && ARRAY[?]::integer[]", items }
And:
def filter(filtering_params)
results = self.where(nil)
filtering_params.each do |key, value|
puts value
if value.is_a?(Array)
value = [0, 1, 2]
results = results.public_send(key, value) if self.where("key && ARRAY[?]::integer[]", value)
else
results = results.public_send(key, value) if value.present?
end
end
results
end
profiles.itemscolumn is an array of integers and you want the query to find rows whoseitemscontain1or0?["0", "1"]