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

时间:2016-10-17 14:22:04

标签: mysql ruby-on-rails activerecord

我想实现这个目标:

    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

1 个答案:

答案 0 :(得分:1)

我认为问题在于你使用双等唱,而应该使用单曲:

joins()

除此之外,查询看起来对我有效(我想你正在做某种JOIN(在includes()子句之前使用wherecurl -s -k -H "Content-Type: audio/x-flac" -H "Authorization: Bearer [my access key]" https://speech.googleapis.com/v1beta1/speech:syncrecognize --data-binary @'file.flac' ))。