【问题标题】:Unable to create a simple model instance with acts_as_tenant无法使用acts_as_tenant 创建简单模型实例
【发布时间】:2015-03-03 13:44:05
【问题描述】:

我正在使用 gem https://github.com/dsaronin/milia。我的一个模型叫做 Group:

class Group < ActiveRecord::Base
  #.....

  acts_as_tenant
end

但由于某种原因我无法创建它。这是 Rails 控制台:

t = Tenant.create(cname: 'cname1', company: 'comp1')
=> #<Tenant id: 3, tenant_id: nil, cname: "cname1", company: "comp1", created_at: "2015-03-03 03:39:38", updated_at: "2015-03-03 03:39:38">
[33] pry(#<Cucumber::Rails::World>)> t.valid?
=> true
[34] pry(#<Cucumber::Rails::World>)> t.new_record?
=> false
[35] pry(#<Cucumber::Rails::World>)> t.errors
=> #<ActiveModel::Errors:0x00000108a5e6d8
 @base=
  #<Tenant id: 3, tenant_id: nil, cname: "cname1", company: "comp1", created_at: "2015-03-03 03:39:38", updated_at: "2015-03-03 03:39:38">,
 @messages={}>

但是说到Group,总是无效的:

> g = Group.new(name: 'name1')
=> #<Group id: nil, name: "name1", room: nil, person_id: nil, created_at: nil, updated_at: nil, tenant_id: nil>
[37] pry(#<Cucumber::Rails::World>)> g.tenant
=> nil
[38] pry(#<Cucumber::Rails::World>)> g.tenant = t
=> #<Tenant id: 3, tenant_id: nil, cname: "cname1", company: "comp1", created_at: "2015-03-03 03:39:38", updated_at: "2015-03-03 03:39:38">
[39] pry(#<Cucumber::Rails::World>)> g.save!
ActiveRecord::RecordInvalid: Gültigkeitsprüfung ist fehlgeschlagen: Tenant muss ausgefüllt werden
from /Users/alex/.rvm/gems/ruby-2.1.2/gems/activerecord-3.2.21/lib/active_record/validations.rb:56:in `save!'
[40] pry(#<Cucumber::Rails::World>)> g.errors
=> #<ActiveModel::Errors:0x00000108888ea8
 @base=
  #<Group id: nil, name: "name1", room: nil, person_id: nil, created_at: nil, updated_at: nil, tenant_id: nil>,
 @messages={:tenant_id=>["muss ausgefüllt werden"]}>
[41] pry(#<Cucumber::Rails::World>)> g.valid?
=> false
[42] pry(#<Cucumber::Rails::World>)> g.new_record?
=> true

这是怎么回事?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 multi-tenant acts-as-tenant


    【解决方案1】:

    我看到的第一件事是您不应该尝试手动创建租户。一个新租户对应于一个新的管理员用户创建,并且在 milia gem 中有定义的代码和过程,您应该遵循这些代码和过程,即使在测试中也是如此。具体来说:租户通过连接表绑定到特定用户(所有者或管理员)。您必须使用文档中概述的 milia 方法来创建新租户,以便正确创建所有内容,包括用户的设计要求。

    其次,“组”对于模型来说是一个糟糕的名称,并且可能与 Rails 关键字冲突。您应该重命名您的模型,因为这最终可能成为错误的根源。您不应该使用 Rails 通用的名称。更具体一些,例如模型的“MyappGroup”。

    因此,如果您尝试创建一个租户/组进行测试,您应该使用固定装置。同样,milia 测试目录中有示例说明如何执行此操作。

    【讨论】:

      猜你喜欢
      • 2021-02-02
      • 2021-08-23
      • 2022-07-20
      • 1970-01-01
      • 2022-11-21
      • 2011-12-20
      • 2020-10-09
      • 1970-01-01
      • 2016-06-21
      相关资源
      最近更新 更多