直接上代码,下面这段是个 SQL 样例。
问题:sql_template
里包含了非参数占位符的%
,导致拼接字符串报错。
sql_template = %q{
select id from users
where username like '%%s%' and departure_date>'%s'
}
self.find_by_sql(sql_template % [rubist, '2014-02-20'])
Google 过大都建议用 Activerecord::Base 提供的 where() 方式。实际系统里存在多表联查使用 db 特性、SQL 片断复用等场景,有时候的确需要这么拼 SQL。
求解,谢谢大伙~~
已经解决:
试出来了,%%
转码%
为普通字符,即可。