在Ruby on rails中,我尝试将数据保存在具有大量关系的多个表中。
我的模特看起来:
evid.rb:
class Evid < ActiveRecord::Base
attr_accessible :kod, :nazov, :okruh_id, :bank_ucets_attributes, :poc_stavs_attributes
#attr_accessible :ciselny_rads_attributes
has_many :ciselny_rads
has_many :bank_ucets, :foreign_key => :evidencia_id
has_many :poc_stavs, :foreign_key => :evidencia_id
accepts_nested_attributes_for :poc_stavs, :allow_destroy => true
accepts_nested_attributes_for :bank_ucets, :allow_destroy => true
#accepts_nested_attributes_for :ciselny_rads, :allow_destroy => true
end
bank_ucet.rb
class BankUcet < ActiveRecord::Base
attr_accessible :ciselny_rad_id, :evidencia_id
attr_accessible :ciselny_rad_attributes
belongs_to :ciselny_rad
belongs_to :evid, :foreign_key => "evidencia_id"
accepts_nested_attributes_for :ciselny_rad, :allow_destroy => true
end
ciselny_rad.rb
class CiselnyRad < ActiveRecord::Base
attr_accessible :evidencia_id, :kod, :nazov, :ciselny_rad_cislos_attributes
belongs_to :evid, :foreign_key => "evidencia_id"
has_many :ciselny_rad_cislos, :dependent => :delete_all
accepts_nested_attributes_for :ciselny_rad_cislos, :allow_destroy => true
end
poc_stav.rb
class PocStav < ActiveRecord::Base
attr_accessible :evidencia_id, :hosp_roky_id, :poc_stav
belongs_to :evid, :foreign_key => :evidencia_id
end
我正在尝试保存数据
@bank_ucet = Evid.create(data)
data={
"kod": "XX",
"nazov": "XxxXxUxXxxX",
"okruh_id": 5,
"bank_ucets_attributes": [
{
"cis_banka_id": 1,
"cislo_uctu": 999999,
"cis_mena_id": 1,
"ciselny_rad_attributes": {
"kod": "XX",
"nazov": "XxxXxUxXxxX"
}
}
],
"poc_stavs_attributes": [
{
"hosp_roky_id": null,
"poc_stav": 1000
}
]
}
一切都很好,但唯一的问题是,表ciselny_rad中的evidencia_id填充为null,而不是新创建的evid。
你能帮帮我吗? 感谢答案 0 :(得分:1)
尝试类似:
has_many :bank_ucets, :foreign_key => :evidencia_id, :inverse_of => :evid
在您的Evid
班级中:
belongs_to :evid, :foreign_key => "evidencia_id", :inverse_of => :bank_ucets
在BankUcet
课程中。
(编辑)推理:根据我使用嵌套属性的经验,在这个地方和附近投掷inverse_of
通常可以解决任何问题..