いくつかの has_many :through アソシエーションを作成したシナリオがありますが、更新に問題があります。どんな助けにも感謝します。
これが私のセットアップです:
- ルビー 1.9.3p392
- レール 3.2.13
シナリオは次のとおりです。
- 関連する3つのモデルがあります
- 多くの業界、多くのキーワード、多くの会議があります
- 業界には多くのキーワードがあります
- 会議には業界があります
- 会議は、その業界に基づいてキーワードを選択します
Keywords には基本的な CRUD モデルがあります。業界を作成してからキーワードを追加するとうまくいきます。
Conferences、Industry、および Keywords に対する私の関係は、私が問題を抱えている場所です。会議の情報を更新し、現在関連付けられている業界を変更し、新しい業界に基づいて新しいキーワードをすべて同じ形式で選択しようとしています。Keywords 要素をミックスから削除すると、更新が機能します。しかし、3 つすべてが存在すると、エラーが発生します。
業種とキーワードに対する私の関係は次のとおりです。
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
ここに Conference/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=", "会議"=>{"名前"=>"テスト会議"," address1"=>"住所 1"、"address2"=>"住所 2"、"都市"=>"都市"、"state_id"=>"38"、"メール"=>"test@example.com"、 "web"=>"www.example.com", "industry_id"=>"1", "industry_attributes"=>{"キーワード"=>["", "6", "5"], "id"= >"3"}}, "commit"=>"Update Conference", "action"=>"update", "controller"=>"カンファレンス", "id"=>"1"}