views:

15

answers:

0

Have you guys ever saved your information with binary masks to improve performance? This is my case and I am also using mongodb with rails to store and retrieve it.

I've used a pair of scopes using '&' and '|' to retrieve users with specific roles, but each time I run those scopes I get all the users regardless the roles... how could I reach this filtering? I've tried this in many different ways of doing it.

Here is the code:

  def self.with_role(role)
    all(:conditions => "(roles_mask & #{2**valid_roles.index(role.to_sym)}) > 0")
  end

  def self.workers
    all(:conditions => "(roles_mask & #{2**valid_roles.index(:developer) + 2**valid_roles.index(:designer) + 2**valid_roles.index(:project_leader)}) > 0")
  end

Note: It's important to mention that I'm using role_model gem to handle bit mask conversion

Thanks in advance!