我有三个模特
用户:
class User < ApplicationRecord
has_many :provider_contracts, :class_name => "Contract", :dependent => :destroy
has_many :provider_users, :class_name => "User", :through => :provider_contracts
has_many :customer_contracts, :class_name => "Contract", :dependent => :destroy
has_many :customer_users, :class_name => "User", :through => :customer_contracts
has_one :profile, :dependent => :destroy
accepts_nested_attributes_for :profile
end
合同:
class Contract < ApplicationRecord
belongs_to :provider_user, :class_name => "Node", :inverse_of => :customer_contracts
belongs_to :customer_user, :class_name => "Node", :inverse_of => :provider_contracts
end
资料:
class Profile < ApplicationRecord
belongs_to :user
end
现在我创建了一个用户名A,我想将一个客户添加到A:
user_a = User.create(:name => "A", :profile_attributes => {:company_name => "A Company"})
# success with profile
user_b = user_a.customer_users.new(:name => "B", :profile_attributes => {:company_name => "B Company"})
user_b.save
# success with contract but profile
结果是:
创建用户A并使用正确的配置文件
创建用户B
用户A和用户B具有正确的合同
但是B的配置文件是在没有user_id的情况下创建的,因此不会创建关联。
请帮助,谢谢!