ruby - How to remove the outer array but still keep the inner elements in their arrays in a Rails SQL string? -
i have array:
array = [["1","one"], ["2","two"], ["3","three"]] how remove outer square brackets, removing 1 dimension array, looks this:
array = ["1","one"], ["2","two"], ["3","three"] i know if wanted flatten entire array, 1 large array, use flatten. however, want rid of outer dimension, while keeping inner elements within respective arrays.
essentially trying query:
phone.where(["brand_name in (?) , os in (?) , price_category in (?)", ["nokia"], ["blackberry", "android"], ["1", "2"]]) but instead, getting. notice 1 more set of array brackets around corresponding column values.
phone.where(["brand_name in (?) , os in (?) , price_category in (?)", [["nokia"], ["blackberry", "android"], ["1", "2"]]]) this method:
def self.checkbox_search(params_hash) params_array = ["brand_name", "os", "price_category"] column_array = [] key_array = [] params_hash.each |k,v| if params_array.include?(k) column_array << v key_array << k + ' in (?)' end end joined_keys = key_array.join(" , ") + ", " + "#{column_array}" phone.where([#{joined_keys}]) end i grabbing params hash, , putting in checkbox_search, goes through hash , puts key values in key_array, , puts values in column_array, if meet specified criteria of key includes params_array. join entire string in joined_keys, put results of joined_keys inside phone.where() string
you're assembling arrays wrong way:
phone.where([ key_array.join(" , ") ] + column_array) that appends column_array values. if inline them they'll pushed down in terms of nesting. note #{...} has no place here, that's used string interpolation , mess things badly.
technically second version equivalent first due how it's parsed , assigned:
x = [1,2],[3,4] # => [[1, 2], [3, 4]] x # => [[1, 2], [3, 4]] that notation's used situations this:
x,y = [1,2],[3,4] # => [[1, 2], [3, 4]] x # => [1, 2] y # => [3, 4] there's no "outer dimension" can remove. either have array of arrays, or have singular array that's flat.
Comments
Post a Comment