Forum: Ruby on Rails Multiple models versus one model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
330edb8dc8bbc5a824984cf46e941b2f?d=identicon&s=25 Tam Kbe (tkbeili)
on 2009-04-10 20:27
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
1e44d920287c3e26c3d7080769b5ccb5?d=identicon&s=25 Jaryl Sim (Guest)
on 2009-04-10 20:34
(Received via mailing list)
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.
330edb8dc8bbc5a824984cf46e941b2f?d=identicon&s=25 Tam Kbe (tkbeili)
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
1e44d920287c3e26c3d7080769b5ccb5?d=identicon&s=25 Jaryl Sim (Guest)
on 2009-04-10 20:53
(Received via mailing list)
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.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-10 21:10
(Received via mailing list)
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.
F7a62f21b0e8b0351742803a2dbcd649?d=identicon&s=25 cpr (Guest)
on 2009-04-11 19:07
(Received via mailing list)
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.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-04-11 20:17
(Received via mailing list)
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
This topic is locked and can not be replied to.