views:

293

answers:

4

User model has three keys: is_master, is_standard, is_guest because I originally wanted to use activerecord boolean methods like is_master? or is_power?.

However, if it would be better to create a UserType relationship and create my own methods like this:

def master?
   return true if self.user_type = 1
end

What's the best practice on something like this? Thanks in advance.

+1  A: 

If the master/standard/guest relationship is mutually exclusive (that is, you can only ever be one of them) then a field that stores the type (in a human-readable form, please -- no opaque numbers) is better. You can always reimplement is_foo? trivially.

On the other hand, if you could have an account that be more than one of master/standard/guest/whatever at once, then stick with the separate boolean fields.

womble
+2  A: 

As a DBA I always hate when people are using columns as flags, it would be a lot of extra columns. If it is all the same type (like account type), I would do as the first anwer suggests (including using text, not numbers).

If you on the other hand needs it for separate flags or multiple types (but having a restricted number) I would actually go for a binary calculation.

That is, have one columns in the table representing all the flags and then assign each flag a number.

ex.

FLAGS = {:master => 1, :standard => 2, :guest => 4, :power => 8, 
         :another_flag => 32, :yet_another_flag => 64}

def is_master?
    self.flags & FLAGS[:master]
end

def is_standard?
    self.flags & FLAGS[:standard]
end

It requires a bit more work when setting the values, but doesn't clutter up the table with a lot of columns used only for flags.

Jimmy Stenke
ejunker
A: 

If you look at how the restful authentication plugin does it (which includes user roles) they use a join table.

I think a join is much more readable.Using a join allows you to be more flexible with your role-system.

If you require that a user can only have a single role I would put that logic in your model.

Bill
A: 

I'd start with the simplest thing that solves your current need. Refactor and add complexity from there as needed. Sounds like a set of boolean columns is just what you need.

Jeremy Weiskotten