Ruby on Rails 3 howto make 'OR' condition

前端 未结 9 1390
無奈伤痛
無奈伤痛 2020-12-03 00:34

I need an SQL statement that check if one condition is satisfied:

SELECT * FROM my_table WHERE my_table.x=1 OR my_table.y=1

I want to do th

9条回答
  •  情歌与酒
    2020-12-03 01:12

    This will works in Rails 5, see rails master :

    Post.where('id = 1').or(Post.where('id = 2'))
    # => SELECT * FROM posts WHERE (id = 1) OR (id = 2)
    

    For Rails 3.0.4+:

    accounts = Account.arel_table
    Account.where(accounts[:id].eq(1).or(accounts[:id].eq(2)))
    

提交回复
热议问题