Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Or conditions in mongoid

I have two queries,

Post.where(:group_id.in => group_ids, :deleted => false) Post.where(:user_id=>user.id,:deleted=>false)

I need to combine these query using or condition. I tried like,

Post.where(:deleted => false).or({:user_id=>user.id},{:group_id.in => group_ids}) and Post.any_of({:group_id.in=>group_ids},{:user_id=>user.id}) but I didn't get results.

like image 744
sssskkk Avatar asked Nov 25 '11 05:11

sssskkk


1 Answers

You can do it like that:

Post.any_of({:group_id.in => group_ids, :deleted => false}, {:user_id => user.id, :deleted => false})

or:

Post.all_of(:deleted => true, :or => [{:group_id.in => group_ids}, {:user_id => user.id}])

You can call selector on any of these expressions to see mongodb query that will be generated to fetch data.

like image 171
KL-7 Avatar answered Nov 10 '22 15:11

KL-7