Thanks for the 'a-ha' moment - I am actually trying to work out how to do just this kind of thing for authentication and privilege granting.
Pardon the Newb follow on question - how would you handle the class issue in a login scenario? With subclasses, I don't know how you could implements something like: If User.isadmin? ... stuff admins can do or see Else ... etc. The Agile Web Dev Book talks about single-table inheritance, but I'm a little confused - is it as simple as just looking at User and letting Rails Magic (TM) figure out the type of user and return the appropriate subclass? Users can log in and Rails works out which subclass to use automagically based on the initial setup of the user in the database? Thanks! SR On Feb 3, 12:56 pm, Sharagoz -- <[email protected]> wrote: > When you have several entities that are almost the same, but not quite, > then using sub- / superclasses might be a good idea. > > When a user is either a regular user or an admin, then the boolean flag > on the user model is fine, but when you start to get several user types, > then I think its better to make subclasses of user for each type. > > Just add a new field to the user model "type" (string) and then you can > create one model for each user type, like > > class SalesPerson < User > end > > Once that is done you can pretty much treat the models as if they had > separate database tables. > If you do SalesPerson.new, then rails will automatically set the type > field to "SalesPerson". If you do SalesPerson.find, rails will know to > look in the users table where type is SalesPerson. > > The great thing here is that you can have separate models for each user > type, without needing a bunch of tables with all the same fields, and > you can add logic to the user model that are inherited by all the user > types for the logic that the they share. > > One warning though: > Dont try to operate on user.type, this will create weired error > messages. > If you ever need to access that field directly, use user['type']. > -- > Posted viahttp://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

