Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#with_tenant method doesn't do it's job for ActiveRecord relations #11

Open
k-rudy opened this issue Apr 20, 2013 · 2 comments
Open

#with_tenant method doesn't do it's job for ActiveRecord relations #11

k-rudy opened this issue Apr 20, 2013 · 2 comments

Comments

@k-rudy
Copy link

k-rudy commented Apr 20, 2013

The problem I have faced is the following: with_tenant method works correctly only for cases where ActiveRecord relation queries db inside the with_tenant block. Otherwise, it is simply ignored.

In other words it works fine here (let's say there are 4 items and only 3 are with the tenant):

Multitenant.with_tenant @tenant do
  @items = Item.where("name IS NOT NULL").to_a 
end
puts @items.count # 3; all correct

But fails to work here:

Multitenant.with_tenant @tenant do
  @items = Item.where("name IS NOT NULL")
end
puts @items.count # 4; incorrect!

Spec that indicates the failure can be found here 81fcb09

@wireframe
Copy link
Owner

This looks to be working as expected since ActiveRecord::Relation is lazily evaluated. The current_tenant attribute is unset after the block has been run, and so invoking count or each outside of the with_tenant block will not add the extra query scope information.

If there is a way to support this usecase, I'm all for it. Do you have any suggestions or patches that work around the issue?

@k-rudy
Copy link
Author

k-rudy commented Jun 27, 2013

@wireframe thanks for your input however personally for me this was not an expected behavior. If this is not gonna be fixed I would recommend adding a sort of note to the readme file about the fact that Relation should be compiled (evaluated) inside the with_tenant block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants