【问题标题】:Rails apartment gem config.excluded_models to include join_table users_rolesRails 公寓 gem config.excluded_models 包括 join_table users_roles
【发布时间】:2015-03-04 15:15:55
【问题描述】:

根据 railscast,我当前的设置是一个多租户应用程序 389-multitenancy-with-postgresql

我正在尝试将设置转移到使用 apartment gem,主要是因为它很好地支持处理跨多个模式的迁移。

我遇到的问题是我使用 cancan 和 rolify gems 导致我的角色模型有这个连接表:users_roles

role.rb
has_and_belongs_to_many :users, :join_table => :users_roles

我想将这个连接表包含在被排除模型的公寓 gem 配置中。这些模型被指定保留在全局(公共)模式中。这是我目前的设置

apartment.rb
config.excluded_models = ["User", "Tenant", "Role" ]

根据公寓网站关于排除型号的信息: 请注意,模型名称的字符串表示现在是标准

那么当 users_roles 表不是模型而只是一个连接表时,我如何将它包含在 exclude_models 列表中?

【问题讨论】:

    标签: ruby-on-rails gem multi-tenant


    【解决方案1】:

    我发现结合 rolify 和公寓 gem 的方法是排除连接模型,就像您所做的那样,并在 rolify 中指定连接表名称,包括 public 架构。

    # apartment.rb
    
    Apartment.configure do |config|
      config.excluded_models = %w{User Tenant Role UsersRole}
    
      # ...
    end
    
    
    # user.rb
    
    class User < ActiveRecord::Base
      rolify role_join_table_name: 'public.users_roles'
    
      # ...
    end
    

    【讨论】:

    • 这对我来说是解决方案,因为 Rolify 似乎没有在 users_roles 连接表中包含“公共”模式。我冒昧地将其重命名为 UserRole。谢谢@troy!
    【解决方案2】:

    我没用过Apartment,但你能从habtm切换到has_manybelongs_to吗?

    换句话说,你有:

    # table 'users'
    class User < ActiveRecord::Base
      has_many :user_roles 
      has_many :roles, :through => :user_roles
    
      # ...rest of class...
    end
    
    # table 'roles'
    class Role < ActiveRecord::Base
      has_many :user_roles
      has_many :users, :through => :user_roles
    
      # ...rest of class....
    end
    
    # table 'user_roles'
    class UserRole < ActiveRecord::Base
      belongs_to :user
      belongs_to :role
    
      # ...rest of class...
    end
    

    然后在 apartment.rb:

    config.excluded_models = ["User", "Tenant", "Role", "UserRole" ]
    

    【讨论】:

    • 这有点晚了,但这绝对是一种解决方案。对于我的场景,它涉及一些其他带有 habtm 的宝石,因此这种方式非常受限制。我过去的解决方案是完全放弃公寓 gem 并自定义整个多租户流程。非常适合作为学习练习。然后,我可以简单地从租户模式中删除仅在公共模式中的任何表。我刚刚又遇到了同样的问题,所以正在重新审视这个问题。
    猜你喜欢
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    相关资源
    最近更新 更多