I'm running into problems when I move this SQL around to different database adapters. I want to convert it into something more flexible. Here it is:
Foo.includes([{ :group => :memberships }, :phone]).
where('("memberships"."user_id" = ? AND "memberships"."owner" = ?)
OR "phone"."user_id" = ?', user.id, true, user.id)
user
is the current user (this query is within a CanCan ability file). The idea is to query all foo's where the current user is the owner of the foo's group, OR the foo's phone is owned by the current user.
I have tried many different queries, but I can't see to get the OR syntax right. Because this is in a CanCan ability, it is not possible to combine two queries, it all needs to be in one large scope like the one above. Is this possible to do?
Group membership is done through the join table memberships
, the owner is designated with the owner
field.
class Foo
belongs_to :group
belongs_to :phone
end