0

I have an app in Rails where I've create a model called User and another called Task.

The model User has an enum for roles, which means that I can have an User that is a client or a developer:

require 'bcrypt'

class User < ApplicationRecord
  has_many :tasks

  enum :role => [
    :client, 
    :developer
  ]

  has_secure_password

  validates_uniqueness_of :document, :email
  validates_presence_of :birthdate, 
                        :credits,
                        :document, 
                        :email, 
                        :name, 
                        :neighborhood, 
                        :phone, 
                        :street, 
                        :street_number, 
                        :zipcode
end

The model Task can be managed by an client or by a developer:

class Task < ApplicationRecord
  enum :priority => [
    :low,
    :medium,
    :high
  ]
  validates_presence_of :description, :title
end

I'm pretty sure that it would be better if I split the table User in two other tables, called Client and Developer, for example, but their attributes are the same and in the future, I want to be able to quickly change the User with the role :client to the role :developer, without having to create a record in the table Developer and destroying the other in the table Client.

My question is: thinking in good practices, is it better to insert two fields in the model Task, one called client_id and the other called developer_id and use the Single-table inheritance or to creating a many-to-many relationship between the model Task and the model User, and just check the role of each user that is being reference in the Task?

Lucas A.
  • 11
  • 4
  • ` I want to be able to quickly change the User with the role :client to the role :developer, without having to create a record in the table Developer and destroying the other in the table Client.` Can you explain ? – Anand Jan 16 '19 at 05:27
  • Common way to handle your case (if I understood all conditions correctly) is: Rolify gem for role management and Pundit/Cancancan for authorisation – Vasilisa Jan 16 '19 at 05:58

0 Answers0