# Newbie question about models

**URL:** <https://www.ruby-forum.com/t/newbie-question-about-models/191199>\
**Category:** Rails\
**Created:** [July 7, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/newbie-question-about-models/191199 "2010-07-07T14:32:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [July 7, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/newbie-question-about-models/191199/1 "2010-07-07T14:32:26Z")

</div>

I am trying to write a system with tables for engineers and system  
users. Both are people. It would seem that:

class Person \< ActiveRecord::Base  
belongs\_to :job, :polymorphic =\> true  
end

class Engineer \< ActiveRecord::Base  
has\_one :person, :as =\> :job  
end

class SystemUser \< ActiveRecord::Base  
has\_one :person, :as =\> :job  
end

would be a way of doing that provided that the two groups did not  
overlap (which of course they do). I am not at all keen to have  
engineer and system user attributes in the person table.

I am sure there is a very simple solution to this but so far research  
has come up with overly complicated answers. Can anyone point me in the  
right direction?

Thanks in advance

Henry

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [July 7, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/newbie-question-about-models/191199/2 "2010-07-07T16:45:15Z")

</div>

Henry O. wrote:

> I am trying to write a system with tables for engineers and system  
> users. Both are people. It would seem that:
> 
> class Person \< ActiveRecord::Base  
> belongs\_to :job, :polymorphic =\> true  
> end
> 
> class Engineer \< ActiveRecord::Base  
> has\_one :person, :as =\> :job  
> end
> 
> class SystemUser \< ActiveRecord::Base  
> has\_one :person, :as =\> :job  
> end

Waiiiit…engineers and system users are _jobs_? That’s what your  
associations would seem to say, unless I’m misreading.

> would be a way of doing that provided that the two groups did not  
> overlap (which of course they do). I am not at all keen to have  
> engineer and system user attributes in the person table.

Do engineers and system users require different sets of fields in their  
respective tables? If not, you could just unify them as a single Person  
class with a role field.

> I am sure there is a very simple solution to this but so far research  
> has come up with overly complicated answers. Can anyone point me in the  
> right direction?
> 
> Thanks in advance
> 
> Henry

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [July 8, 2010, 9:50am UTC](https://www.ruby-forum.com/t/newbie-question-about-models/191199/3 "2010-07-08T09:50:29Z")

</div>

Marnen

Thanks for the reply.

> Waiiiit…engineers and system users are _jobs_? That’s what your  
> associations would seem to say, unless I’m misreading.

Yes. Though perhaps role is a better choice of word.

> Do engineers and system users require different sets of fields in their  
> respective tables?  
> Yes. So many that I don’t want to have redundant fields in a single  
> table

Henry

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [July 8, 2010, 10:51am UTC](https://www.ruby-forum.com/t/newbie-question-about-models/191199/4 "2010-07-08T10:51:42Z")

</div>

Colin

Thanks for your reply.

Colin L. wrote:

> > So many that I don’t want to have redundant fields in a single  
> > table
> 
> Why?

Because I oversimplified and there are actually many roles, each with  
many attributes. As I understand it STI _is_ an all in one table with a  
role (conventionally “type”).

Is the real answer that rails doesn’t adapt well to what I want to do?  
I can’t believe that.

By the way I have found the has\_many\_polymorphs plugin but it seems like  
a sledgehammer to crack a very small nut.

Henry

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [July 8, 2010, 10:06am UTC](https://www.ruby-forum.com/t/newbie-question-about-models/191199/5 "2010-07-08T10:06:03Z")

</div>

On 8 July 2010 08:50, Henry O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Do engineers and system users require different sets of fields in their  
> > respective tables?  
> > Yes. So many that I don’t want to have redundant fields in a single  
> > table

Why? The simplest solution is usually the best. How many million  
users will you have if disc space is your worry? You might want to  
look at STI if you want to keep the types functionally distinct,  
though again the simplicity of an all in one table with a role is  
usually preferable. You will easily find a plugin that will handle  
roles for you to make life even easier. The less code you write the  
fewer bugs you will have and the easier it will be to code, test and  
maintain.

Colin
