gpt4 book ai didi

ruby-on-rails - Rails、范围、OR 和连接

转载 作者:行者123 更新时间:2023-12-03 15:07:50 25 4
gpt4 key购买 nike

我有一个范围:

includes(:countries).where("profiles.sector = :sector OR advices.sector = :sector", :sector => sector)

它生成以下 SQL:
SELECT `profiles`.* FROM `profiles` INNER JOIN `advices` ON `advices`.`profile_id` = `profiles`.`id` WHERE (profiles.sector = 'Forestry_paper' OR advices.sector = 'Forestry_paper')

(是的,我的 ProfileCountry 模型中有国家/地区)

不幸的是, OR似乎失败:

它不会呈现只有适当部门但没有相关建议的配置文件。想法?

最佳答案

您正在执行 INNER JOIN,因此它要求配置文件具有相应的建议。请尝试以下操作:

Profile
.joins("LEFT JOIN advices ON advices.profile_id = profiles.id")
.where("profiles.sector = :sector OR advices.sector = :sector", :sector => sector)

这还将包括没有建议的配置文件。

关于ruby-on-rails - Rails、范围、OR 和连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5047065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com