condition = "status = #{val} OR quality = #{other_val}"
def func(condition)
Table.where(condition).other_queries
end
我需要这个功能,因为other_queries是常用的,我不想重复代码。
问题是当val
或other_val
为空时,我收到无效的SQL错误。查询字符串基本上类似于"status = OR quality = 4"
在这种情况下我该怎么办?
答案 0 :(得分:2)
condition = []
condition << "status = #{val}" if val
condition << "quality = #{other_val}" if other_val
def func(condition)
(condition.empty? ? Table : Table.where(condition.join(' OR '))).other_queries
end