require_tenant doesn't throw on controller index of newly scaffolded model

Donn Felker
Not sure if this is working as intended, but from what I'm able to gather from the acts_as_tenant gem, it should be throwing a ActsAsTenant::NoTenant error. 

Steps to repro: 

  1. Scaffold out new model (e.g. A projects model), add account_id and multitenancy acts_as_tenant/belongs to values
  2. Set require_tenant to true in acts_as_tenant intialializer
  3. Restart app
  4. Visit /projects URL in incognito browser
  5. All projects show
  6. Open Rails Console, type "Project.all", you'll get the NoTenant error. 

Seems to be an issue with sort_by_params not respecting the tenant. 

... or I'm misunderstanding the usage/etc. Thoughts? 
Donn Felker
I think know the problem. I'm using subdomains. The first account I created in the config does not have a subdomain set up, (it is nil in the db). 

In the set_current_request_details.rb file we check for the subdomain (I'm running on localhost:5000, the root domain) and this line of code is executing: 

if Jumpstart::Multitenancy.subdomain?
  Current.account ||= Account.find_by(subdomain: request.subdomains.first)
end

The request.subdomains.first is nil. The "Account.find_by(...)" is looking for a subdomain that is nil. Since the subdomain in my only account is nil, its using that one. Therefore, the account is set (the tenant is set), which is why when I attempt to load it in a signed out manner, the app still finds a tenant. 

If I change the code to this: 

if Jumpstart::Multitenancy.subdomain? && request.subdomains.first != nil
  Current.account ||= Account.find_by(subdomain: request.subdomains.first)
end

Then it works as I would expect and I get a NoTenant error. However, I'm not sure if thats the best place to check for it. 

Opinions on this? 
Chris Oliver
Turn on `require_tenant` for ActsAsTenant. https://gitlab.com/gorails/jumpstart-pro/-/blob/master/config/initializers/acts_as_tenant.rb#L5

Since it depends on how you intend to use it, we just keep it using the defaults.
Chris Oliver
Missed that in your post, you already did that. 😅

If you want to, you can require subdomain presence on accounts so you don't have any without. That would make sure your situation with a nil subdomain doesn't match an Account.
Donn Felker
True. Thanks Chris
Notifications
You’re not receiving notifications from this thread.
© 2022 Jumpstart Pro by GoRails, LLC