简体   繁体   中英

Unable to create a simple model instance with acts_as_tenant

I'm using a gem https://github.com/dsaronin/milia . One of my models is called Group:

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

  acts_as_tenant
end

But I can't create it for some reason. Here's rails console:

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={}>

But when it comes to Group, it's always invalid:

> 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

What's up with it?

First thing I see is that you should NOT be trying to manually create a tenant. A new tenant corresponds to a new admin user creation and there is defined code and procedure in the milia gem for that which you should follow, even in testing. Specifically: a tenant is tied to a specific user (the owner or admin) via a join table. You must use the milia methods, outlined in the documentation for new tenant creation so that everything gets correctly created, including the devise requirements for a user.

Secondly, "Group" is a poor name for a model and probably conflicts with a Rails keyword. You should rename your model, as that might eventually become the source of an error. You shouldn't use names common to Rails. Be more specific, such as, "MyappGroup" for the model.

So, if you are trying to create a tenant/group for testing, you should use fixtures. Again, there are samples in the milia test directory for how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM