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

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

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

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:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs