opened
joel
username_0
Consider 2 clients `client_instance_one` and `client_instance_two` , Article have tenant with optional option for keep possibility to create Article without tenant.

```
class Article
  include Mongoid::Document
  include Mongoid::Multitenancy::Document

  tenant(:client, optional: true)

  field :title, :type => String
end
```

Create 1 Article belong to first client like your example in the README
```
Mongoid::Multitenancy.with_tenant(client_instance) do
  article = Article.new(:title => 'New article 1')
  article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: nil>

  # tenant needs to be set manually
  article.tenant = client_instance_one
  article.save # => <#Article _id: 50ca04b86c82bfc125000044, title: 'New blog', client_id: 50ca04b86c82bfc125000025>
end
```

We have following expected behavior 
```
Mongoid::Multitenancy.current_tenant = client_instance_one
Article.all.map(&:title) # => ['New article 1']
```

Create a new article without tenant
```
Mongoid::Multitenancy.current_tenant = nil
article = Article.new(:title => 'New article 2')
article.save
Article.all.map(&:title) # => ['New article 1', 'New article 2']
```

First tenant list this articles
```
Mongoid::Multitenancy.current_tenant = client_instance_one
Article.all.map(&:title) # => ['New article 1', 'New article 2']
```

This is strange behavior for me, but can be ok so far. We will assign a tenant to the second article
```
Mongoid::Multitenancy.current_tenant = client_instance_two
article.tenant = client_instance_two
article.save
Article.all.map(&:title) # => ['New article 2']
```
but still visible with the scope of previous client
```
Mongoid::Multitenancy.current_tenant = client_instance_one
Article.all.map(&:title) # => ['New article 1', 'New article 2']
```
This is really what we are excepted? With this behavior it's impossible to delegate later the assignment of resource like creation of user and scoping them after for avoid patch of devise for example...
Avoid Devise patching
issue
created
joel
username_0
Obviously this works : 

``` 
Mongoid::Multitenancy.current_tenant = client_instance_one
Article.where(client: Mongoid::Multitenancy.current_tenant).map(&:name)
# => ['New article 1']
``` 

But we need to add a new scope outside of your gem....
comment
created
joel
username_0
We can add our expected behavior with the following scope : 
`scope :authorized, ->{ where(client: Mongoid::Multitenancy.current_tenant) }` but I think is a missing part here
comment
created
abrisse
username_1
@username_0 : using `optional: true`when declaring the tenant association means that the article does not necessarily belongs to a specific tenant and therefore is *shared* among all the tenants.

So when you execute:

```ruby
Mongoid::Multitenancy.current_tenant = nil
article = Article.new(:title => 'New article 2')
article.save
Article.all.map(&:title) # => ['New article 1', 'New article 2']
```

Article.all returns all the articles the current tenant has access to, ie both articles.

Concerning the second part, `article.tenant = client_instance_two` cannot work since the tenant field is `client`. So it should be instead `article.client = client_instance_two. If you do that, `Article.all.map(&:title)` will only return `['New article 1']` as expected.
comment
closed
abrisse
username_1
issue