I'm trying to get the following request sanitized to send to a MySQL server:
INSERT INTO `table`
SELECT NULL, t.`id`, ?, ?
FROM `table` AS t
WHERE t.`some_field` = ?
The tricky part is that that request is to be executed in a class that is not my model class. It looks like this:
class Model < ActiveRecord::Base
def some_method
Service.new(self).run
end
end
class Service
def initialize(model)
@model = model
end
def run
# Here is the request
end
end
I've seen a lot of people using Model#sanitize_sql
, but this is a protected method, which is unusable in my context.
Any idea?
EDIT:
It has been suggested that my question might be a duplicate of this one. I've seen this question before posting, but the answers provided there aren't relevant in my case: I don't want to use quote
because most of my fields are going to be numeric values. The other answer suggests not using raw SQL, but, as stated in the comments, I don't think ActiveRecord is capable of generating an INSERT...SELECT
query. (This question seems to confirm it)