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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Max Williams
2008-Apr-03 11:54 UTC
Re: Mapping a model to a table to some records of the table
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, > PankajI''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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: http://api.rubyonrails.org/classes/ActiveRecord/Base.html On Apr 3, 4:50 am, pankaj <pankajbhage...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---