I have 2 ruby/mysql database structure questions:
A – The set-up
I have 2 objects, (e.g., employees and documents), in this example, I would like to allow some employees to access other employee’s documents. The access should also have permission levels (view, edit, full).
Employee:
class Employee < ActiveRecord::Base
has_many :documents, :dependent => :delete_all
end
and Document:
class Document < ActiveRecord::Base
belongs_to :employee
end
B – The options
I am trying to decide which is the better database/best practice for how to do this. I see two options:
Option 1) Employee could have linked documents.
In this case, the document owner is adding access to the other employee (i.e. the one employee is editing the other employee)
Employee has many linked documents. (simple one-to-many relationship edited by the document owner) For data structure, I could follow this example:
Rails Many to Many with Extra Column
In the documents controller, index, I would select all documents, I would do something simple:
Select employee.documents, and employee.linked_documents, two datasets, loaded at the time the employee is.
Option 2) Documents could have linked employees.
In this case, the document is edited by its owner, granting access to other people. The Document would have many linked users (simple one-to-many relationship edited by the document owner).
Like above the data structure would be like the linked article.
The select statements are not as clean. The initial select can be the same, select employee.documents, but the other would be some sort of select documents.where(document.linked_user = current user).
C – My efforts
I am leaning towards option 2 because the document is edited by the employee, rather than employees editing another employee.
In my document controller, show, I have some logic to ensure the document is owned by the employee:
if document.owner != current user redirect to no access. I imagine I would need to refactor this to a document level property. if doccument.accessallowed == false redirect to no access. It seems that adding this to the document would reduce cluttering the controller with “if this” or “if that”s.
D – The Ask:
- Which is better, option 1, or option 2?
- Is the document level access property the right solution for either option?