Closed dv closed 7 years ago
After rubberducking this here, I figured I should re-research the Rails 5 codepath. I first thought the subs/bindings are used for prepared statements but after further investigation that's not the case. I came up with the following which seems to do the trick. I'll refactor and test this a bit and use that as a solution.
values = record.send(:arel_attributes_with_values_for_create, record.attribute_names)
model = record.class
scope = model.unscoped
im = scope.arel.create_insert
im.into model.arel_table
substitutes, binds = scope.substitute_values(values)
im.insert substitutes
record.class.connection.to_sql(im, binds)
For posterity's sake: It actually does have something to do with prepared statements (doh), and can be fixed by surrounding the last statement with model.connection.unprepared_statement
. There's probably a less roundabout way to do it, but I've used up my time budget for this issue and found a Good Enough (tm) solution.
We're trying to make the POLO gem work with Rails 5. Polo is a gem that generates
INSERT
statements for the records that already exist in the database.After a cursory look through the codepaths, I can't figure out how to avoid the following deprecation notice:
After looking through how Rails does it, and some other documentation, I've come up with the following few lines that should generate the
INSERT
statement, however the deprecation notice still happens (at the last line):It seems from this line that it's actually impossible not to call
quote
without a column, unless the value is anSqlLiteral
. The deprecation notice talks about aQuoted
but I'm not sure that's relevant for our usecase (I tried wrapping each value in aQuoted
, then we got the error "can't quote quoted").Is it possible to avoid the deprecation notice when generating an INSERT statement using Arel? I must be overlooking something but can't figure it out. Any help would be appreciated.