# Multiple models versus one model

**URL:** <https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251>\
**Category:** Rails\
**Created:** [April 10, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251 "2009-04-10T20:27:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 10, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251/1 "2009-04-10T20:27:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 10, 2009, 8:42pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251/2 "2009-04-10T20:42:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 10, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251/3 "2009-04-10T20:34:35Z")

</div>

Instead of using inheritance, you should use a role based solution.  
One of the cleanest I’ve used is Acl9,

> **[GitHub - be9/acl9: Yet another role-based authorization system for Rails](https://github.com/be9/acl9/tree/master)**
>
> 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.

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 10, 2009, 8:53pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251/4 "2009-04-10T20:53:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 10, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251/5 "2009-04-10T21:10:20Z")

</div>

Tam K. 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.

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 11, 2009, 7:07pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251/6 "2009-04-11T19:07:50Z")

</div>

[http://stonean.com/projects/lockdown](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.

---

<div class="post-metadata">

**Author:** ![tkbeili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7bcc69/32.png) [@tkbeili](https://www.ruby-forum.com/u/tkbeili)\
**Post date:** [April 11, 2009, 8:17pm UTC](https://www.ruby-forum.com/t/multiple-models-versus-one-model/165251/7 "2009-04-11T20:17:32Z")

</div>

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 J.
