1

I have an array of filters in a ruby application which makes a raw SQL query to a Postgres connection. I'm trying to figure out how I can serialize this array into something query-able,

i.e.: SELECT * FROM data WHERE strings IN #{strings_array}

I can't find any resources for this. What's the correct way to serialize this query?

Edit: I ended up figuring out:

query = ''
arr.each_with_index { |e, i|
  if i == arr.length - 1
    query += "#{e}"
  else
    query += "#{e},"
  end
}

then

" AND column @> ('{#{query}}')"

But there must be a less verbose way, no?

2
  • You want to serialize the array of data or the query over that data? Commented Feb 6, 2015 at 8:11
  • @Jesper - I want to query a variable array of data. For example, I have an array of strings like names = ['Jason', 'Bob', 'Chris'] and I want to embed names into my query string, so that I can match a column first_names where the values in the names array are in that row Commented Feb 6, 2015 at 8:16

3 Answers 3

3

You say that you use Sequel. Sequel can perform this kind of query automatically:

DB[:data].where(strings: ["my", "array", "of", "strings"])
# => #<Sequel::Postgres::Dataset: "SELECT * FROM \"data\" WHERE (\"strings\" IN ('my', 'array', 'of', 'strings'))">

Constructing SQL queries "by hand" is risky, especially if you're dealing with data coming from users.

Sign up to request clarification or add additional context in comments.

Comments

2

This can be less verbose if you use join method like this :

# I presume arr contains an array that you want serialize
strings_array = "'#{arr.join("','")}'"
query = "SELECT * FROM data WHERE strings IN #{strings_array}" 

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@splrs I update my answer for a better comprehension
-2

Once you learn to know ORM (Activerecord in this case) you won't be constructing SQL manually or semi-automatic like you do. Take this example, It creates a connection to a database (Oracle in this case), maps the table to a Ruby class and gives all kind of manupulation methods to that class. In this example I use two where's, one with a hash as filter and one with a tiny bit of SQL. The result will be a SQL that is automatically generated and executed once you do something with it (use each in this case).

require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => 'oracle_enhanced',
  :database => "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = ORATCP.world)(PROTOCOL = TCP)(Host = server.domain)(Port = 9999)))(CONNECT_DATA = (SID = SID_ID)))",
  :username => 'user',
  :password => 'password'
)

class Member < ActiveRecord::Base
  self.table_name = 'my_table'
end

Member.where(country: 'Belgium').where("'gender = 'M'").each do |member|
  p member
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.