我想执行一个更新原始sql,如下所示:
update table set f1=? where f2=? and f3=?
Run Code Online (Sandbox Code Playgroud)
这个SQL将由执行ActiveRecord::Base.connection.execute,但我不知道如何将动态参数值传递给方法.
有人可以给我任何帮助吗?
我知道准备好的语句,但如果我使用原始SQL,ActiveRecord是否有办法手动转义值?
像这样的东西会很好:
self.escape("O'Malley") # O\'Malley
Run Code Online (Sandbox Code Playgroud) 我想在ActiveRecord中使用子查询,如下所示:
User.select(
'users.*,
(select sum(amount)
from subscriptions
where subscriptions.user_id = user.id
and created_at < ? ) as amt)'
).order('amt')
Run Code Online (Sandbox Code Playgroud)
但是,在倒数第二行,我有一个问题,我无法弄清楚如何绑定Time类参数,因为ActiveRecord::Base's select方法不接受多个参数(sql字符串).我该怎么办?