Hacker News new | ask | show | jobs
by sbayona573 3456 days ago
What's the benefit of this approach vs doing scoped queries? i.e. current_user.pages.find(params[:id])
3 comments

(1) It makes so you can't forget.

(2) Some customers require it.

(3) At scale, database sharding becomes necessary. Multi-tenancy is a natural way to shard.

My experience is that it's easy for a developer (new or not) to forget to scope some query. So, by using a gem like this or tenancy, your infrastructure prevents the tragic mistake of missing it.

A middle ground solution might extend active record to warn you whenever a query doesn't have current_user / current_customer.

The issue is that you would be missing the tenant_id (or user_id) if you were to do `other_object.pages.find(params[:id])` (i.e. on something thats not directly below the user)

That is a problem in systems that expect the tenant_id to always be in the query, so you can locate which node the query needs to go to.