我有以下三種模式(滑軌2.3.8)與自己的屬性連接表使用accepts_nested_attributes_for - 重複行
class Outbreak < ActiveRecord::Base
has_many :incidents, :dependent => :destroy
has_many :locations, :through => :incidents
accepts_nested_attributes_for :incidents, :allow_destroy => true
accepts_nested_attributes_for :locations, :allow_destroy => true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
class Incident < ActiveRecord::Base
belongs_to :outbreak
belongs_to :location
end
class Location < ActiveRecord::Base
has_many :incidents
has_many :outbreaks, :through => :incidents
accepts_nested_attributes_for :incidents, :allow_destroy => true
end
從表格中的參數似乎是好
「outbreak」=> { 「locations_attributes」=> {「0」=> {「lon」=>「 - 1.39」,「placename」=>「wetwe」,「hpu_id」=>「15」 >>「」,「地址_2」=>「」,「地址_3」=>「」,「地址_id」=>「10」,「地址_1」=>「」,「城鎮」=> lat「=>」50.89「}},」incidents_attributes「=> {」0「=> {」subtype_id「=>」7「,」category_id「=>」1「,」detail「=>」「 ,「subcategory_id」=>「2」}} }
但是,當保存爆發時,在爆發和位置表的Incidents表(連接表)和單行中創建3行。
在事件表中的行沒有完全從PARAMS填充如下:
id outbreak_id location_id category_id subcategory_id subtype_id detail created_at updated_at
57 23 NULL 1 2 7 2010-11-25 14:45:18.385905 2010-11-25 14:45:18.385905
58 23 27 NULL NULL NULL NULL 2010-11-25 14:45:18.39828 2010-11-25 14:45:18.39828
59 23 27 NULL NULL NULL NULL 2010-11-25 14:45:18.403051 2010-11-25 14:45:18.403051
這一定是由於參數的任何格式或多個accepts_nested_attributes_for方法 - 我怎麼剛纔在Incidents表中輸入一行所有參數信息?
非常感謝這一點,因爲我與雙向映射struggeling有:通過,以及...但你可以發佈您的創建操作和如果可能的話,也可以使用表格(僅供參考)。 – tbk 2010-12-02 12:35:26