我有一个场景,我创建了一些 has_many :through 关联,但我无法更新。我很感激任何帮助。
这是我的设置:
- 红宝石 1.9.3p392
- 导轨 3.2.13
这是场景:
- 我有 3 个相关的模型
- 行业多,关键词多,会议多
- 行业有很多关键词
- 会议有一个行业
- 会议根据其行业选择关键字
关键字有一个基本的 CRUD 模型。创建一个行业然后添加关键字可以正常工作。
我与会议、行业和关键字的关系是我遇到问题的地方。我正在尝试更新会议信息,更改当前关联的行业,根据新行业选择新关键字,所有这些都以相同的形式。当我从组合中删除关键字元素时,更新工作。但是当所有三个都在那里时,我就会得到错误。
我对行业和关键字的关系是:
class IKeywordable < ActiveRecord::Base
attr_accessible :industry_id, :keyword_id
belongs_to :industry
belongs_to :keyword
end
class CKeywordable < ActiveRecord::Base
attr_accessible :conference_id, :keyword_id
belongs_to :conference
belongs_to :keyword
end
以下是模型:
class Industry < ActiveRecord::Base
attr_accessible :name,
:conferences_attributes,
:i_keywordables_attributes,
:keywords,
:keywords_attributes,
:keyword_ids
validates :name, presence: true
validates :name, uniqueness: true
# Associations
has_many :conferences
accepts_nested_attributes_for :conferences
#Keywords
has_many :i_keywordables
has_many :keywords, through: :i_keywordables
accepts_nested_attributes_for :i_keywordables
accepts_nested_attributes_for :keywords
end
class Keyword < ActiveRecord::Base
attr_accessible
:name, :profile_id, :active, :rating,
:c_keywordables_attributes,
:i_keywordables_attributes,
:industries_atributes
# Associations
#Industries
has_many :i_keywordables
has_many :industries, through: :i_keywordables
accepts_nested_attributes_for :i_keywordables
#Conferences
has_many :c_keywordables
has_many :conferences, through: :c_keywordables
accepts_nested_attributes_for :c_keywordables
end
class Conference < ActiveRecord::Base
attr_accessible
:name, :address1, :address2, :city, :email,
:web, :profile_id, :state_id, :conference,
:description, :date_start, :date_end, :venue, :venue_web,
:organizer, :organizer_web, :c_keywordables_attributes,
:industry_attributes, :industry_id,
:industry, :keyword_ids
# Associations
belongs_to :state
belongs_to :industry
accepts_nested_attributes_for :industry
#Keywords
has_many :c_keywordables
has_many :keywords, through: :c_keywordables
accepts_nested_attributes_for :c_keywordables
serialize :keywords, Array
def to_s
state_id
end
end
这是会议/edit.html.erb
<h2>edit <%= @conference.name %></h2>
<%= form_for @conference do |c| %>
<%= c.label :name %>
<%= c.text_field :name %>
<%= c.label :address1 %>
<%= c.text_field :address1 %>
<%= c.label :address2 %>
<%= c.text_field :address2 %>
<%= c.label :city %>
<%= c.text_field :city %>
<%= c.label :state_id%>
<%= c.collection_select :state_id, State.order(:name), :id, :name %>
<%= c.label :email %>
<%= c.text_field :email %>
<%= c.label :web %>
<%= c.text_field :web %>
<hr width="80%">
<%= c.label :industry_id %>
<%= c.collection_select :industry_id, Industry.order(:name), :id, :name %>
<br />
<%= c.fields_for :industry, @industry do |ci| %>
<%= ci.label :keyword_id %>
<%= ci.grouped_collection_select :keywords, Industry.order(:name),
:keywords,
:name,
:id,
:name,
{:include_blank => true}, { :multiple => true }
%>
<% end %>
<br />
<p><%= c.submit %></p>
<% end %>
这是会议控制器:
def edit
@conference = Conference.find(params[:id])
end
def update
@conference = Conference.find(params[:id])
if @conference.update_attributes(params[:conference])
redirect_to action: "all", notice: 'conference was successfully updated.'
else
render action: "edit"
end
end
视图呈现正常,但是当我提交更改时,出现错误。我看到的错误是:
无法为 ID=1 的会议找到 ID=3 的行业
以下是传递的参数:
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"k9fcAXF/iOXt3oVuTcxeDCaWOfE2PrVlBvFbQG8Mo/I=", "conference"=>{"name"=>"测试会议", " address1"=>"address 1", "address2"=>"address 2", "city"=>"City", "state_id"=>"38", "email"=>"test@example.com", "web"=>"www.example.com", "industry_id"=>"1", "industry_attributes"=>{"keywords"=>["", "6", "5"], "id"= >"3"}}, "commit"=>"更新会议", "action"=>"update", "controller"=>"会议”,“id”=>“1”}