使用搜索关联表的 LIKE 创建 Rails 查询
Create Rails query using LIKE that searches over association tables
我想使用 Postgresql 'LIKE' 编写一个 Rails 查询。我希望能够搜索当前 table 的列以及关联 table.
我可以这样搜索当前 table:
User.where("description LIKE ?", "%happy%")
我可以这样搜索协会:
User.joins(:products).where("products.description LIKE ?", "%happy%")
如何将这 2 个查询合并为一个?我想 return 所有描述包含 "happy" and/or 的用户有一个描述包含 "happy".
的产品
和版本
User.joins(:products).where("users.description LIKE ? AND products.description LIKE ?", "%happy%", "%happy%")
或版本
User.joins(:products).where("users.description LIKE ? OR products.description LIKE ?", "%happy%", "%happy%")
我想使用 Postgresql 'LIKE' 编写一个 Rails 查询。我希望能够搜索当前 table 的列以及关联 table.
我可以这样搜索当前 table:
User.where("description LIKE ?", "%happy%")
我可以这样搜索协会:
User.joins(:products).where("products.description LIKE ?", "%happy%")
如何将这 2 个查询合并为一个?我想 return 所有描述包含 "happy" and/or 的用户有一个描述包含 "happy".
的产品和版本
User.joins(:products).where("users.description LIKE ? AND products.description LIKE ?", "%happy%", "%happy%")
或版本
User.joins(:products).where("users.description LIKE ? OR products.description LIKE ?", "%happy%", "%happy%")