Reputation: 16462
So within rails, using active record, we can have multiple models that inherit user
Base Class - User Sub-Class - Employee, Manager, Supervisor
So in rails we use only one table though when you create a new Employee and when you try to access Employee.salary, though only managers and supervisors should have access to those attributes. How do you protect those? Attr methods?
Thanks in Advance.
Upvotes: 2
Views: 1069
Reputation: 24783
I set up STI in a project a long time ago. I forget the details now but here is some code from that project that might help. This is from Rails 2.3.4. Not sure what it would look like in Rails 3. By default Rails with assume any column called type is there for Single Table Inheritance purposes. If you need to specify use the "set_inheritance_column" as you see below. I don't recall being able to protect the attributes of one subclass from the other subclasses. Like you mentioned, its all stored in the same table. I think I tried at the time and couldn't but I was very new to Rails at the time. I would think it might be doable with attr_accessable or something on the subclass. I might be wrong though but give it a try.
class User < ActiveRecord::Base
set_inheritance_column :user_type
attr_protected :user_type
end
class BusinessOwner < User
has_many :businesses
end
class SiteUser < User
end
From the schema:
create_table "users", :force => true do |t|
t.string "user_type"
t.string "username"
t.string "email"
t.integer "location_id"
...
end
Upvotes: 2