# Mapping a model to a table to some records of the table

**URL:** <https://www.ruby-forum.com/t/mapping-a-model-to-a-table-to-some-records-of-the-table/133761>\
**Category:** Rails\
**Created:** [April 3, 2008, 10:51am UTC](https://www.ruby-forum.com/t/mapping-a-model-to-a-table-to-some-records-of-the-table/133761 "2008-04-03T10:51:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [April 3, 2008, 10:51am UTC](https://www.ruby-forum.com/t/mapping-a-model-to-a-table-to-some-records-of-the-table/133761/1 "2008-04-03T10:51:13Z")

</div>

I have a users table and a Users model  
there Employee model which derives from the User model.  
Employee model should only work on the records in user table which  
have role\_id =1.  
What is the solution.  
Regards,  
Pankaj

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [April 3, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/mapping-a-model-to-a-table-to-some-records-of-the-table/133761/2 "2008-04-03T13:54:14Z")

</div>

pankaj wrote:

> I have a users table and a Users model  
> there Employee model which derives from the User model.  
> Employee model should only work on the records in user table which  
> have role\_id =1.  
> What is the solution.  
> Regards,  
> Pankaj

I’m not sure if you can do exactly what you want. However, you can set  
up associations that are conditional in this way. So, for example, if  
you have ‘companies’ and companies have employees, then you can set up  
this association in Company.rb

has\_many :employees,  
:class\_name =\> “User”,  
:conditions =\> [“role\_id = 1”]

This requires all of the users to have a company\_id, and of course for  
you to have a Company model, with at least one instance for them to  
reference in their company\_id.

Now, you can get the users who are employees by saying

@company.employees

If you don’t have a company model, then i assume that all of your users  
work for the same company. In which case, i’d recommend creating a  
Company class with a single entry corresponding to your company: after  
all, it doesn’t make much sense to have an Employee model without  
anything to employ them.

Hope this helps

---

<div class="post-metadata">

**Author:** ![pankaj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a9adbd/32.png) [@pankaj](https://www.ruby-forum.com/u/pankaj)\
**Post date:** [April 3, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/mapping-a-model-to-a-table-to-some-records-of-the-table/133761/3 "2008-04-03T16:40:59Z")

</div>

If I understand what you’re saying, User is a superclass of Employee  
(so every Employee is a User, but not every User is not necessarily an  
Employee). For that type of solution you should look into Single  
Table Inheritance. Rather than use role\_id, you simple use ‘type’.  
The column will track whether the record represents a User (type=nil)  
or an Employee (type=‘Employee’).

Take a look at the ActiveRecord::Base documentation under the ‘Single  
Table Inheritance’ entry:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.
