I'm trying to create a query with variables from a map function, but the content stored in one of these fields does contain '
(quotes, like Barney's). So everytime it cracks since the '
will break the statement. How can I get around it?
I tried to use the .split
function but no sucess.
No worries about SQL Injection since I'm just loading data from an API to my db.
Code:
query_values = activities.map do |activity|
'(' +
"#{activity['id']},
""'#{activity['type']}""'" #using ""' just to fill the column when empty cells are raised
+')'
end
query = "INSERT INTO pd_activities VALUES #{query_values.join(', ')}"
Thanks in advance.
How to do this properly is listed on the cheat sheet:
db[:pd_activities].insert(
id: activity['id'],
type: activity['type']
)
This takes care of all the escaping issues for you. If all activity
has is those two keys you might even be able to do this:
db[:pd_activities].insert(activity)