Reputation: 15863
I have a User model in which their are two booleans for admin and manager statuses, each manager can have only one job created, and each job can have many managers. I made a model for the jobs which have its attributes. I need a way to handle the relationship between the manager user and the job model. My problem here is that I don't need users with admin or without manager statuses to have a relationship with the job model, how to accomplish that?
Upvotes: 0
Views: 28
Reputation: 3627
class User
attr_accessible :is_admin, :is_manager, job_id
belongs_to :job
validate :only_manager_belongs_to_job
private
def only_manager_belongs_to_job
if !is_manager && job_id
errors.add(:job_id, "Only a manager can have a job")
end
end
end
class Job
has_many :managers, class_name: "User"
end
STI is overkill for this (and most other things it's used for).
Upvotes: 1
Reputation: 29349
Have you thought about Single Table Inheritance?. You will have to add type column and job_id column to users table
class User < ActiveRecord::Base
end
class Manager < User
belongs_to :job
end
class Admin < User
end
class Job < ActiveRecord::Base
has_many :managers
end
Upvotes: 1