質問

使用時は新しい accepts_nested_attributes_for ActiveRecordには、このオプションを使用し :allow_destroy => true.このオプションを設定し、ハッシュを含む入れ子のような属性 {"_delete"=>"1", "id"=>"..."} 渡される update_attributes を削除するメソッドは、入れ子のオブジェクトです。

簡単セットアップ:

class Forum < ActiveRecord::Base
  has_many :users
  accepts_nested_attributes_for :users, :allow_destroy => true
end

class User < ActiveRecord::Base
  belongs_to :forum
end

Forum.first.update_attributes("users_attributes"=>{"0"=>{"_delete"=>"1", "id"=>"42"}})

質問 い-削除するのではなく、入れ子の物体の場合 "_delete" => "1" けを削除します。(上記の場合を設定しforum_idのユーザーがnil)

ボーナスの質問: になっているのでしょうか?ものを変更したい属性をネストしたオブジェクトが削除されました。(例:のように設定した状態またはタイムスタンプ)

役に立ちましたか?

解決

うユーザの削除を "_delete" => '1', まず更新するとともにnested_attributes?:

Forum.first.update_attributes("users_attributes"=> { 
  "0" => {
    "id" => "42",
    "forum_id" => "",
    "state" => 'removed'
  }
})
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top