在Rails中,类似于Laravel中的orWhereHas方法,可以使用or关键字来连接多个where条件。

假设有两个模型,Product和Sku,Product拥有多个Sku。想要查询包含特定关键字的Product,并且关联的Sku中的title或description也包含该关键字,可以使用以下方法:

Product.joins(:skus).where("products.title LIKE ? OR products.description LIKE ? OR skus.title LIKE ? OR skus.description LIKE ?", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%")

这里的关键字为keyword,使用%进行模糊匹配。使用joins方法连接两个模型,并使用where方法添加多个条件,使用OR关键字连接不同的条件。

orWhereHasskus function $query use $like $query-wheretitle like $like -orWheredescription like $like; ;rails中类似orwherehas的写法

原文地址: https://www.cveoy.top/t/topic/idTh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录