1

Has anyone successfully used the Rails acts_as_tenant gem for multitenancy, where the tenant ID column is not named xxx_id?

my model is something like

has_one :tenant, :foreign_key => "tenant_code" 
acts_as_tenant :tenant

Then I get my query select * from [table] where tenant_id = xxx ignoring the overridden foreign_key spec.

Is there any way to fix this? or am I doing something else obviously wrong?

Thanks!!

wrschneider
  • 17,913
  • 16
  • 96
  • 176

2 Answers2

2

Just as an update to this issue for future reference:

From gem version 0.3.3 onwards it is now possible to explicitly set the foreign key.

acts_as_tenant(:account, :foreign_key => 'accountID')

Perhaps this helps someone who finds this question.

ErwinM
  • 5,051
  • 2
  • 23
  • 35
1

ActsAsTenant sets it's own foreign key in the gem itself as (pseudocode) #{tenant_klass}_id:

def self.fkey
  "#{@@tenant_klass.to_s}_id"
end

https://github.com/ErwinM/acts_as_tenant/blob/master/lib/acts_as_tenant/model_extensions.rb#L12-L14

Unfortunately, I don't believe you can set your own foreign key for anything other than activerecord model relationships (with your code, you could still theoretically call model.tenant; however, that doesn't help you).

rpearce
  • 1,720
  • 1
  • 21
  • 29