Question

user inputs to text_field_tag e.g. a,b,c are seen as one value1 i.e. value1 = 'a,b,c' in a SQL IN operator (value1, value2, value3,...,) instead of value1='a', value2='b' and value3='c'.

I'm using sequel's db.fetch to write the SQL. Splits and Joins and their associated regexp formats don't seem to give the form 'a','b','c', i.e. separate values in a SQL IN Operator.

Any thoughts?

Was it helpful?

Solution

Assuming you have some user input as a string:

user_input = 'a,b,c'

and a posts table with a value1 column. You can get posts with values a, b or c with the following query:

values = user_input.split(',')
#=> ["a", "b", "c"]

DB = Sequel.sqlite
#=> #<Sequel::SQLite::Database: {:adapter=>:sqlite}>

dataset = DB[:posts]
#=> #<Sequel::SQLite::Dataset: "SELECT * FROM `posts`">

dataset.where(:value1 => values).sql
#=> #<Sequel::SQLite::Dataset: "SELECT * FROM `posts` WHERE (`value1` IN ('a', 'b', 'c'))">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top