gpt4 book ai didi

ruby-on-rails - 在具有自己属性的连接表上使用 accepts_nested_attributes_for - 重复行

转载 作者:行者123 更新时间:2023-12-05 02:25:07 29 4
gpt4 key购买 nike

我有以下三个模型(Rails 2.3.8)

    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

表单的参数好像没问题

“爆发”=>{"locations_attributes"=>{"0"=>{"lon"=>"-1.39", "placename"=>"wetwe", "hpu_id"=>"15", "postcode"=>"so1 1aa", "region_id"=>"10", "address_1"=>"", "town"=>"Bargate", "address_2"=>"", "address_3"=>"", "lat"=>"50.89"}},"incidents_attributes"=>{"0"=>{"subtype_id"=>"7", "category_id"=>"1", "detail"=>"", "subcategory_id"=>"2"} }

但是当保存爆发时,会在事件表(连接表)中创建 3 行,并在爆发和位置表中创建一行。

Incidents 表中的行未完全由参数填充,如下所示:

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 方法 - 我如何才能在事件表中输入包含所有参数信息的一行?

最佳答案

本周到目前为止,我已经第二次回答了我自己的问题 ^^ 这将教会我在放弃和在网上发帖寻求帮助之前付出更多努力,

在查看了我最初的问题后,我仍然没有包含足够的信息来正确回答它 - 这个问题(除了模型的设置)归结为 Outbreak Controller 新方法中的 Outbreak 构造函数,

原始 Outbreaks_controller

def new

@outbreak = Outbreak.new
@outbreak.risks.build
//links locations directly to Outbreak instead of through Incidents
@outbreak.locations.build
@outbreak.incidents.build

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @outbreak }
end
end

修改后的 Outbreaks_controller

def new

@outbreak = Outbreak.new
@outbreak.risks.build
//builds Incidents then a Location through that incident
@outbreak.incidents.build.build_location

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @outbreak }
end
end

三个模型的变化

    class Outbreak < ActiveRecord::Base
has_many :incidents, :dependent => :destroy
has_many :locations, :through => :incidents

accepts_nested_attributes_for :incidents, :allow_destroy => true


end

class Incident < ActiveRecord::Base
belongs_to :outbreak
belongs_to :location

accepts_nested_attributes_for :location, :allow_destroy => true
end


class Location < ActiveRecord::Base
has_many :incidents
has_many :outbreaks, :through => :incidents

end

这似乎工作正常 - 还发布了创建操作和主窗体

关于ruby-on-rails - 在具有自己属性的连接表上使用 accepts_nested_attributes_for - 重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279028/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com