0

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
tereško
  • 58,060
  • 25
  • 98
  • 150
Logan Serman
  • 29,447
  • 27
  • 102
  • 141
  • Have you tried the same query but with single quotes arround the table/attribute names? Like this: `where("('memberships'.'user_id' = ? AND 'memberships'.'owner' = ?) OR 'phone'.'user_id' = ?", user.id, true, user.id)` – MrYoshiji Jan 17 '13 at 20:03
  • Will that be compatible across SQLite, MySQL, and Oracle? – Logan Serman Jan 17 '13 at 20:28
  • Well I had problems with double quotes and MySQL - PostGreSQL, using single quotes solved it. I'm hoping this solution can work for you too – MrYoshiji Jan 17 '13 at 20:33

0 Answers0