Ruby on rails 3 Rails 3添加附加联接关系无法设置主键
我有四个具有以下关联的表和一个嵌套表单,可以正确创建和保存每个模型的一条记录。在添加一个额外的嵌套级别以包含新的联接模型后,由于联系人模型上未设置company_id,因此我的表单无法保存 原始工作模式: 公司:Ruby on rails 3 Rails 3添加附加联接关系无法设置主键,ruby-on-rails-3,nested-forms,model-associations,jointable,Ruby On Rails 3,Nested Forms,Model Associations,Jointable,我有四个具有以下关联的表和一个嵌套表单,可以正确创建和保存每个模型的一条记录。在添加一个额外的嵌套级别以包含新的联接模型后,由于联系人模型上未设置company_id,因此我的表单无法保存 原始工作模式: 公司: Company < ActiveRecordBase attr_accessible :companyContacts_attributes, :company_name, :created_by, :import_path, :is_active, :is_tracker
Company < ActiveRecordBase
attr_accessible :companyContacts_attributes, :company_name, :created_by, :import_path, :is_active, :is_tracker_client, :tracker_code, :tracker_import_path, :updated_by,
:locations_attributes, :contacts_attributes
has_many :locations, dependent: :destroy # A company has one or more locations
has_many :addresses, through: :locations
has_many :contacts
accepts_nested_attributes_for :locations, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :addresses
accepts_nested_attributes_for :contacts
attr_accessible :companyContacts_attributes
has_many :companyContacts
has_many :locations, through: :companyContacts, dependent: :destroy
has_many :contacts, through: :companyContacts, dependent: :destroy
accepts_nested_attributes_for :companyContacts
地点:
Location < ActiveRecordBase
attr_accessible :address_attributes, :address, :created_by, :is_active, :location_name, :location_type_id, :region_id, :updated_by, :website
belongs_to :company
belongs_to :address
accepts_nested_attributes_for :address, :reject_if => :all_blank
has_many :companyContacts
has_one :company, through: :companyContacts
联系人:
has_many :companyContacts
has_one :company, through: :companyContacts
然后,我使用字段_将位置和联系人表单输入嵌套在CompanyContact中。
一切看起来都很好,但当我保存事务时,返回一个错误,该错误显示:
无法将值NULL插入表的“公司id”列中
'TakeBackDBMSConversion.recycle.Contact';列不允许空值。
插入失败。:EXEC sp_executesql N'INSERT to[recycle]。[联系]
([公司id]、[创建地点]、[创建人]、[名字]、[处于活动状态],
[last_name]、[title]、[Update_at]、[Update_by])值(@0、@1、@2、,
@3, @4, @5, @6, @7, @8); 选择CAST(SCOPE_IDENTITY()作为bigint)作为
Ident',N'@0 int,@1 datetime,@2 varchar(50),@3 varchar(50),@4位,
@5瓦查尔(50),@6瓦查尔(100),@7日期时间,@8瓦查尔(50),@0=
空,@1='2014-02-13T21:25:09.337',@2=N'Chris Reid',@3=
N'Sean',@4=0,@5=N'Jackson',@6=N'CEO',@7=
'2014-02-13T21:25:09.337',@8=NULL
传递给控制器的参数看起来嵌套正确:
{“utf8”=>“✓", "真实性\u令牌“=>”xxxBLAHBLAHBLAHBLAH“,
“公司”=>{“公司名称”=>“假公司”,
“companyContacts_attributes”=>{“0”=>{“位置_属性”=>{“地址_属性”=>{“地址线_1”=>”676
假项次“,“地址线2=>”,“地址线3=>”,
“城市”=>“移动”、“州”=>“AL”、“国家”=>“美国”,
“zipcode”=>“89998”},“location\u type\u id”=>“1”,
“位置\名称”=>“移动”},
“联系人属性”=>{“名字”=>“肖恩”、“姓氏”=>“杰克逊”,
“title”=>“CEO”}}},“提交”=>“保存信息”}
为什么公司id突然(意味着现在我已经添加了CompanyContact关联)不可用于联系人记录?我在建立新协会的过程中错过了什么吗?尝试了多种解决方案后,我不知所措。任何帮助都将不胜感激
attr_accessible :companyContacts_attributes
has_many :companyContacts
has_many :locations, through: :companyContacts, dependent: :destroy
has_many :contacts, through: :companyContacts, dependent: :destroy
accepts_nested_attributes_for :companyContacts
has_many :companyContacts
has_one :company, through: :companyContacts
has_many :companyContacts
has_one :company, through: :companyContacts