How can I make an 'OR' statement in ActiveRecord?

前端 未结 2 749
暗喜
暗喜 2021-01-03 02:20

I am trying to create an \'OR\' sql statement in ActiveRecord 3, I tried all kinds of variations but can\'t figure it out...

For example I want this query to include

相关标签:
2条回答
  • 2021-01-03 02:37

    Try this:

    Post.where("posts.user = ? OR posts.channel_id IN (?)", "mike", ids)
    
    0 讨论(0)
  • 2021-01-03 03:02

    Use the Arel methods to do this:

    t = Post.arel_table
    ids = [1,2,3]
    
    Post.where(
      t[:user].eq("mike").or(t[:channel_id].in(ids))
    )
    
    0 讨论(0)
提交回复
热议问题