I trying to create the following scope:
scope :involving, -> (user) do
where("conversations.sender_id =? OR conversations.recipient_id =?", user.id, user.id)
end
but I get:
wrong number of arguments (3 for 1)
What I'm doing wrong.
Thanks in advance.
Mongoid's
where
doesn't understand SQL snippets likea = ? OR b = ?
, it only wants a single hash argument. Hence your "3 for 1" error.You want to use
any_of
to build an:$or
query:Beware that multiple
any_of
s are combined so this:is saying:
rather than
as you might expect.
If you're expecting to every combine OR conditions then you might want to combine the
sender_id
andrecipient_id
into a single array field:so that you could say:
and avoid the OR-problem. You'd still have separate
sender_id
andrecipient_id
fields of course, you'd just duplicate the data to better fit your queries. Denormalizing your data to suit your queries is quite common with MongoDB.