I could not find any direct way to do it. So did use this hack.
User.all(:conditions => ['permission & ? != 0 and permission & ? != 0', 1,2])
Question
I have a user table (datamapper model) that has a column called permission
which contains the bitmask value.
property :permission, Flag[:perm1, :perm2, :perm3]
I want to find all the users who have certain permissions, such as perm1 and perm2
so I call,
User.all(:permission => [:perm1, :perm2])
This makes query
select * from user where permission = 3
that is incorrect.
while correct query should have been (because it is type - flag
)
select * from user where permission &1 != 0 and permission &2 != 0
Does anyone in ruby datamapper, how to make the call to search in flag values.
Solution
I could not find any direct way to do it. So did use this hack.
User.all(:conditions => ['permission & ? != 0 and permission & ? != 0', 1,2])
OTHER TIPS
Which version are you running? Under 1.2, I get SELECT ... FROM "users" WHERE "permission" IN (1, 2) ...
with User.all(:permission => [:perm1, :perm2])
.
One option is to make a union: User.all(:permission => :perm1) | User.all(:permission => :perm2)
.
Or maybe shortened to User.perm1s | User.perm2s
by class methods:
class User
# ...
def self.perm1s; all :permission => :perm1 end
def self.perm2s; all :permission => :perm2 end
end
Not exactly the same query with either one as you've shown, but the result should be the same.