我有一个带有users
表格和user_profiles
表格的应用程序。一个用户has_one
用户配置文件和一个用户配置文件belongs_to
一个用户。
我想确保关联场景始终为真,因此我对两个外键的存在进行了验证。问题是我遇到了“鸡和蛋”的情况。当我创建用户时,它不起作用,因为用户配置文件还不存在,当我创建用户配置文件时,它也不起作用,因为用户还不存在。所以我需要在创建用户的过程中创建用户配置文件。更复杂的是,当我创建客户端时,我还在after_create
回调中创建了一个用户。说够了(或读/写),这里有一些代码:
class User < ActiveRecord::Base
has_one :user_profile
validates :user_profile_id, presence: true
end
class UserProfile < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
end
class Client < ActiveRecord::Base
after_create :create_client_user
private
def create_client_user
User.create!(
email: "admin@example.com",
password: "admin",
password_confirmation: "admin",
client_id: self.id
# I need to create a user profile dynamically here
)
end
end
有可能做我想做的事吗?
更新
我尝试了@cdesrosiers 建议的解决方案,但我的规格无法通过。我主要有三个错误。首先让我向您展示更新的模型:
class User < ActiveRecord::Base
has_one :user_profile, inverse_of: :user
before_create { build_user_profile }
validates :user_profile, presence: true
def client=(client)
self.client_id = client.id
end
def client
current_database = Apartment::Database.current_database
Apartment::Database.switch
client = Client.find(self.client_id)
Apartment::Database.switch(current_database)
client
end
end
class UserProfile < ActiveRecord::Base
belongs_to :user
validates :user, presence: true
end
class Client < ActiveRecord::Base
attr_accessible :domain, :name
after_create :create_client_database
after_create :create_client_user
after_destroy :drop_client_database
# Create the client database (Apartment) for multi-tenancy
def create_client_database
Apartment::Database.create(self.domain)
end
# Create an admin user for the client
def create_client_user
Apartment::Database.switch(self.domain)
User.create!(
email: "admin@example.com",
password: "admin",
password_confirmation: "admin",
client: self
)
# Switch back to the public schema
Apartment::Database.switch
end
def drop_client_database
Apartment::Database.drop(self.domain)
end
end
我正在使用 FactoryGirl 创建工厂,这是我的工厂文件:
FactoryGirl.define do
factory :client do
sequence(:domain) { |n| "client#{n}" }
name Faker::Company.name
end
factory :user do
sequence(:email) { |n| "user#{n}@example.com"}
password "password"
password_confirmation "password"
client
#user_profile
end
factory :credentials, class: User do
email "user@example.com"
password "password"
end
factory :user_profile do
forename Faker::Name.first_name
surname Faker::Name.last_name
birthday (5..90).to_a.sample.years.ago
#user
end
end
如果我分别取消注释用户和用户配置文件工厂中的user_profile
和关联,我会得到一个.user
WARNING: out of shared memory
现在,当我创建其中一个工厂时,我得到了以下三个错误之一:
Failure/Error: @user = create(:user)
ActiveRecord::RecordInvalid:
Validation failed: User profile A user profile is required
# ./app/models/client.rb:41:in `create_client_user'
# ./spec/controllers/users_controller_spec.rb:150:in `block (4 levels) in <top (required)>'
Failure/Error: create(:user_profile).should respond_to :surname
ActiveRecord::RecordInvalid:
Validation failed: User A user is required
# ./spec/models/user_profile_spec.rb:29:in `block (4 levels) in <top (required)>'
Failure/Error: let(:client) { create(:client) }
ActiveRecord::RecordInvalid:
Validation failed: User profile A user profile is required
# ./app/models/client.rb:41:in `create_client_user'
# ./spec/controllers/sessions_controller_spec.rb:4:in `block (2 levels) in <top (required)>'
# ./spec/controllers/sessions_controller_spec.rb:7:in `block (2 levels) in <top (required)>'
所以我假设 User 模型中的更改不起作用。另请注意,我user_profile_id
从 users 表中删除了 。