jekuno / milia

Easy multi-tenanting for Rails5 (or Rails4) + Devise
MIT License
341 stars 72 forks source link

What is the thinking behind using Thread to store current tenant #61

Closed msaspence closed 9 years ago

msaspence commented 9 years ago

This isn't really an issue but I curious to know why you have chosen to store the current tenant in the Thread.

Is this not the equivalent to a global variable? Am I expect to maintain my own current tenant id in either the session or the user object and move it into the Thread variable at the beginning of each request?

dsaronin commented 9 years ago

Yes, equivalent to a global variable; but there is no other way to handle having this information around whether in controller, model, or view. Multi-tenanting really should be handled and enforced at the DBMS level; but unfortunately, none of the FOSS DBMs can handle this properly (row/table locking, current tenant defined, etc). So it has to be handled at a higher level and in a consistent, somewhat hidden from the casual programmer. milia elected to use Rails' default_scope and a thread variable. In many cases this works well and doesn't interfere with typical Rails applications.

Please read the milia documentation for how you should handle initializing the current_tenant at the start of each request. There is a specific way of doing that and I should not have to repeat myself in this comment.