一緒にリンクする必要がある3 つのテーブル ( products
、custom_attribute
、 ) があります。、、およびattribute_value
を持つattribute_value_maps
テーブルがあります。product_id
custom_attribute_id
attribute_value_id
私の製品モデルaccepts_nested_attributes_for :attribute_value_maps
とaccepts_nested_attributes_for :attribute_values
.
私の製品フォームには、次のような新しい attribute_values のフィールドがあります。
<input id="product_attribute_values_attributes_1_custom_attribute_id" name="product[attribute_values_attributes][1][custom_attribute_id]" value="1" type="hidden">
<input id="product_attribute_values_attributes_1_attribute_value_name" name="product[attribute_values_attributes][1][name]">
コントローラーはベーシック@product.update_attributes(paras[:product])
。
これにより、attribute_value
レコードが適切に作成されますが、attribute_value_map
レコードは nil で保存されcustom_attribute_id
ます。
質問 1:attribute_value_map
この方法でレコードを適切に保存する簡単な方法はありcustom_attribute_id
ますか?
質問 2:既に map レコードがあったとします。 name の入力を追加するとproduct[attribute_values_attributes][1][attribute_value_map_id]
、有効なフィールドではないと怒鳴られます。新しい attribute_value の作成中に既存のマップ レコードを更新するにはどうすればよいですか?
編集 1: 詳細をリクエスト
製品モデル:
has_many :custom_attributes, :through => :attribute_value_maps
has_many :attribute_value_maps, :dependent => :destroy
has_man :attribute_values, :through => :attribute_value_maps
accepts_nested_attributes_for :attribute_value_maps
accepts_nested_attributes_for :attribute_values
custom_attribute モデル
has_many :attribute_value_maps
attribute_value モデル
belongs_to :custom_attribute
has_many :attribute_value_maps
attribute_value_map モデル
belongs_to :product
belongs_to :custom_attribute
belongs_to :attribute_value