PerfectMemory/mongoid-multitenancy

Can't create user without tenant

Closed this issue · 4 comments

I use devise gem. There are Users::Base and Users::SuperAdmin < Users::Base.
Tenant defined in Users::Base class, like this: tenant :tenant, class_name: 'School', optional: true.
And when i try to create admin without tenant errors appear: admin.errors => #<ActiveModel::Errors:0x000055d12b92a4f8 @base= #<Housefactions::Users::SuperAdmin _id: BSON::ObjectId('5f046730a55b358791d33176'), age: nil, avatar_content_type: nil, avatar_file_name: nil, avatar_file_size: nil, avatar_fingerprint: nil, avatar_updated_at: nil, created_at: nil, email: "admin@housefactions.com", first_name: "Super", last_name: "Admin", number: nil, registration_ip: nil, sex: nil, tenant_id: nil, updated_at: nil>, @details={:tenant=>[{:error=>:blank}, {:error=>:blank}]}, @messages={:tenant=>["can't be blank", "can't be blank"]}>

Hi @kladaFOX ,

Did you resolve your issue ? You set tenant :tenant, class_name: 'School', optional: true but School should be rather Users::Base, no ?

Aymeric

Hi @kladaFOX ,

Did you resolve your issue ? You set tenant :tenant, class_name: 'School', optional: true but School should be rather Users::Base, no ?

Aymeric

Hi @abrisse
No I didn't.
There are several schools in database, and every school have its users, either students or admins

And i would like to create admin for all the scopes that i have. To manage students from all schools.

Hello @kladaFOX,

Cannot reproduce:

class School
  include Mongoid::Document

  field :name, type: String
end

module Users
  class Base
    include Mongoid::Multitenancy::Document
    include Mongoid::Document

    field :name, type: String
    tenant :tenant, class_name: 'School', optional: true
  end

  class SuperAdmin < Base
  end
end

Mongoid::Multitenancy.with_tenant(School.create!(name: 'UTC')) do
  pp Users::SuperAdmin.create!(name: 'John').reload
end

Mongoid::Multitenancy.with_tenant(nil) do
  pp Users::SuperAdmin.create!(name: 'John').reload
end

displays

#<Users::SuperAdmin _id: 60746938e9ed112a3d48dccc, name: "John", tenant_id: BSON::ObjectId('60746938e9ed112a3d48dccb'), _type: "Users::SuperAdmin">
#<Users::SuperAdmin _id: 60746938e9ed112a3d48dccd, name: "John", tenant_id: nil, _type: "Users::SuperAdmin">