Comments (13)
Note that when I put the acts_as_tenant(:account) before the :has_and_belongs_to_many association, the problem doesn't occur. But when I put it below the :has_and_belongs_to_many association in the model. The problem occurs.
The reason that I want to put it below the :has_and_belongs_to_many association is that I want to make an inheritance of the model and the child model will be assigned to acts_as_tenant. So, the code in the parent model will be executed first and following by the child model code.
Could you tell me how to solve this problem? Thank you in advance.
from acts_as_tenant.
Could you post your model code for me?
from acts_as_tenant.
Thank you for replying me. Here are my models.
class Account < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
belongs_to :account
has_and_belongs_to_many :roles
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
end
class Member < User
acts_as_tenant(:account)
end
from acts_as_tenant.
In the code above acts_as_tenant has nothing to do with the HABTM association: acts_as_tenant is defined on the member model which does not have an HABTM association.
Can you also post the code that triggers the 'undefined method'-error, or post the stack trace?
from acts_as_tenant.
I have tracked to the code and the error comes from the model_extensions.rb.
71 reflect_on_all_associations.each do |a|
72 unless a == reflection || a.macro == :has_many || a.macro == :has_one || a.options[:polymorphic]
73 # check if the association is aliasing another class, if so
74 # find the unaliased class name
75 association_class = a.options[:class_name].nil? ? a.name.to_s.classify.constantize : a.options[:class_name].constantize
76 validates_each a.foreign_key.to_sym do |record, attr, value|
77 # Invalidate the association unless the parent is known to the tenant or no association has
78 # been set.
79 record.errors.add attr, "is invalid (Acts_as_Tenant)" unless value.nil? || association_class.where(:id => value).present?
80 end
81 end
82 end
I think in this block code, it passes through the "unless" block because the association is HABTM and it gets an error in the "validates_each" block.
from acts_as_tenant.
One more thing, it happens just only on the update operations. (eg. update_attributes)
from acts_as_tenant.
I think this is happening because your Role model is not scoped to account. Could you try scoping the Role model to account as well and see if you get the error?
from acts_as_tenant.
Thank you so much for answering. In the role model, why I need to scope to :account, because I don't need :roles table to be scoped. It is generic for every tenant. I mean for every user they will use the same roles in the table.
from acts_as_tenant.
yup, I understand that. It would help me figure out if that's the problem though..
from acts_as_tenant.
Right now, my code is changed. I decided to remove the roles table from my project and use the role_mask attributes instead.
Note that... If I add "|| a.macro == :has_and_belongs_to_many" to the line 72. It can solve the problem too. But I am not sure whether it affects the security? or am I doing the right thing.
Note2. in the error it said "undefined method user_id". So, I don't that why it involves with user_id.
from acts_as_tenant.
@ErwinM When I added the account_id in Role, the problem is disappeared. I think the problem has occur in case of the :belongs_to macro. So what should I do?
from acts_as_tenant.
@torsakch I think it's actually a problem with the gem: the gem currently assumes that every association will always be scoped to the tenant. In practice, a scoped model can be associated with a non-scoped model (like in your case).
I will need to fix this in the code. I'll try to push out an update tonight.
from acts_as_tenant.
This should actually be fixed now. Let me know if it isn't.
from acts_as_tenant.
Related Issues (20)
- Acts as tenant through a belongs to association HOT 3
- User can access everything (admin) HOT 1
- I want to set config.require_tenant to false when the path is /admin, but it doesn't work. HOT 3
- Find Subdomain or Domain, prefer Domain First? HOT 1
- require_tenant set in initializer; not able to use ::with_tenant block without throwing NoTenantSet exception
- avoiding Postgres bitmap heap scans HOT 2
- How to set user before require_tenant check? HOT 1
- Is there a way to disable in the console? HOT 2
- No need to based on Rails MVC HOT 2
- Saving wrong tentant when using factory bot
- Enabling eager_loading causes NoTenantSet error HOT 3
- Gotcha & workaround when using require_tenant = true with ActionMailer::Preview's HOT 5
- Best practice for job queues? HOT 2
- Documentation issue with README.md spotted a mistake HOT 1
- Question: Migration to CurrentAttributes. HOT 2
- ActiveJobExtensions clobbers Sidekiq tenancy HOT 1
- Tenant ID overriden when saving without notice
- ActsAsTenant unexpectedly blocks generation of ActiveStorage representation HOT 5
- Issues with default_url_options HOT 5
- Change to CurrentAttributes leads to broken ActiveJob testing
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from acts_as_tenant.