gpt4 book ai didi

mysql - 在没有 Arel 的情况下如何在 Rails 中编写此查询

转载 作者:行者123 更新时间:2023-11-29 11:13:48 25 4
gpt4 key购买 nike

我想实现这个:

    where(
"(memberships.id IN (?) AND memberships.user_id == ?)
OR (memberships.user_id == ? AND communities.hidden == ?)",
m, user.id, nil, false)

这返回一个错误:

ActiveRecord::StatementInvalid:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= 77 OR memberships.user_id == NULL AND communities.hidden == 0)' at line 1: SELECT DISTINCT `communities`.* FROM `communities` LEFT JOIN `memberships` ON `communities`.`id` = `memberships`.`community_id` AND `memberships`.`user_id` = 77 WHERE `communities`.`deleted_at` IS NULL AND (memberships.id IN (NULL) AND memberships.user_id == 77 OR memberships.user_id == NULL AND communities.hidden == 0)

我不想使用 Arel

最佳答案

我认为问题是你使用的是双等号,而应该使用单号:

where("(memberships.id IN (?) AND memberships.user_id = ?) OR (memberships.user_id = ? AND communities.hidden = ?)", m, user.id, nil, false)

除此之外,查询对我来说看起来是有效的(我想你正在做某种 JOIN (在 之前使用 joins()includes() ) where 子句))。

关于mysql - 在没有 Arel 的情况下如何在 Rails 中编写此查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40088923/

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