Thomas
Thomas

Reputation: 2386

Rails Database Structure Advice?

I need to make a website structure in rails in which there are administrators, workers, and clients. The administrators need to have control over everything and be able to view everything, workers need to have control over just the clients and be able to view all the clients information, and clients have no control over anything, but can view their own data.

Whats the best way to accomplish this?

I was thinking about making an administrator model which has_many :workers and has_many :clients and then creating a worker model which has_many :clients and belongs_to :administrator and then creating a client model which belongs_to :worker. Is this the most efficient way to do this?

Upvotes: 0

Views: 49

Answers (1)

Suborx
Suborx

Reputation: 3669

The standard way is make User and Role models. Then add some authentification gem like devise and also autorization gem like cancan where you can setup abilities for specific roles.

in your case

class Role < ActiveRecord::Base
  has_many :users
end

class User < ActiveRecord::Base 
  belongs_to :role
end

class Ablility
  include CanCan::Ability  

  def initialize(user) 
    case user.role.name.to_sym
    when :admin          
      can :manage, :all
    when :worker
      can :manage, User, :role => { :name => 'client' }
    when :client
      can :read, User, :user_id => user.id
    end
  end
end

Upvotes: 2

Related Questions