Object.merge在SQL查询中创建AND - 什么方法创建OR?

时间:2015-01-30 11:12:35

标签: sql ruby-on-rails

以下使用合并的代码生成下面的SQL。我用粗体标记了我想要改变的操作符。

   Micropost.from_users_followed_by(self).merge(
    Micropost.direct_replies_to(self))

SELECT" microposts"。* FROM" microposts"在哪里"微博"。"回复_" ='示例-1' AND (user_id IN(SELECT followed_id FROM relationships  WHERE follower_id = 2)AND user_id = 2)ORDER BY microposts.created_at DESC

什么方法在两者之间产生OR运算符?即:

SELECT" microposts"。* FROM" microposts"在哪里"微博"。"回复_" ='示例-1' OR (user_id IN(SELECT followed_id FROM relationships  WHERE follower_id = 2)AND user_id = 2)ORDER BY microposts.created_at DESC

1 个答案:

答案 0 :(得分:0)

对一列值的OR运算符,可以将数组传递给Where子句,使用ActiveRecord使用IN(value1,value2):

一列 Micropost.where(:reply_to => ["value1", "value2"])

了解更多专栏 Micropost.where("reply_to = ? or follower_id = ?", 'example-1', 2)

或试试这个

Micropost.where("(reply_to = 'example-1') or (another_condition))