【发布时间】:2013-08-01 09:39:16
【问题描述】:
我需要在 rails 中运行这样的查询:
select users.* from users inner join posts on users.id = posts.user_id where posts.title like '%xxx%' or posts.title like '%yyy%' or posts.title like '%zzz%'
User 和 Post 是我的模型。我做了这样的事情:
title_array = ["xxx", "yyy", "zzz"]
users = User.all
users = users.joins(:posts).where(posts: { title: title_array })
但这给了我这样的 sql 查询:
select users.* from users inner join posts on users.id = posts.user_id where posts.title IN ('%xxx%', '%yyy%', '%zzz%')
我还有一个数组中的 posts.title 的值。因此,我给出了三个 OR 值的示例,但它可能因每个请求而异。
我需要 rails 方法来解决这个问题。有人可以帮我解决这个问题吗?
【问题讨论】:
标签: sql ruby-on-rails arel