Hello, I have a design question concerning my app with Rails. I have two types of users let's call them Type1 and Type2. They both have very similar attributes but different associations and functionality. So for every type of user I will need different "has_many, has_one,...etc" Is it better to create one model and have another column called "type" knowing that whenever the model is loaded the associations for bother types will be loaded or is it better to create two separate models and have every model gets its own associations? I care about performance but if it's not significant then it doesn't matter as much. I also care about complexity in case the application expands in future. Thanks, Tam
on 2009-04-10 20:27
on 2009-04-10 20:34
Instead of using inheritance, you should use a role based solution. One of the cleanest I've used is Acl9, http://github.com/be9/acl9/tree/master This will also fit your needs for future complexity since roles are abstracted out of the model and into a separate table. So if you ever need to add more roles, it wouldn't change your database design at all.
on 2009-04-10 20:42
Thanks Jaryl, But would using role-base solution lower the performance of my app by having to load the associations for all roles every time the model is loaded? Thanks, Tam
on 2009-04-10 20:53
There will definitely be a performance hit, but I don't know if it will make much of a difference in your situation. I can't provide any hard evidence, but there are many ways you can go about improving performance for loading associations. I'm sure you are aware of eager loading; you can start with that.
on 2009-04-10 21:10
Tam Kbe wrote: > Thanks Jaryl, > > But would using role-base solution lower the performance of my app by > having to load the associations for all roles every time the model is > loaded? Google "premature optimizations". There's generally no way to guess which part of your code will be slow, so if you keep it clean and easy to change you can then fix the parts that actually slow it down.
on 2009-04-11 19:07
http://stonean.com/projects/lockdown does roles without loading from the DB on every access. I like how the specification of all permissions and groups are in one place.
on 2009-04-11 20:17
Role-based auth is one possibility, but single-table inheritance is another. It's very dependent on how your app handles permissions; if a user can have a mixture of different permissons, role-based is better. On the other hand, if your app has clearly delimited user categories (ie Employee vs. Client, etc) inheritance may be preferable. You can then specify the common associations in the base User class, while putting any extra info in the subclasses. --Matt Jones