Combining multiple named scopes with OR

后端 未结 2 1043
旧时难觅i
旧时难觅i 2020-12-12 01:26

I am trying to combine two scopes or add some more to an existing scope.

scope :public_bids, -> {
  where(status: [Status::ACCEPTED, Status::OUTBID, Statu         


        
2条回答
  •  天涯浪人
    2020-12-12 01:48

    Rails 4 and older do not support OR query "natively". Looks like it will be introduced in Rails 5.

    For now, you would have to use SQL with .where method:

    YourModel.where('field1 = ? OR field2 = ?', 1, 2)
    

    But, in your case, you are selecting using IN and = queries and the first field is in both scopes. So, it would not make much sense. Anyway, if you really have to, you might get away with this:

    data = YourModel.public_bids.outbid_maxbids
    YourModel.where(data.where_values.inject(:or))
    

提交回复
热议问题