2

私のモデルスキーマ:

投票

has_many :questions
has_many :responses :through => :questions

質問

belongs_to :poll
has_many :responses

応答

belongs_to :question

実行しようとしたとき、または@poll.responses.delete_allこのエラーが発生したときの問題:cleardestroy_all

ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'Poll#responses' because the source reflection class 'Response' is associated to 'Question' via :has_many.

更新:これが発生している理由はまだわかりませんが、回避策は次のとおりです。 @poll.responses.each(&:destroy)

4

2 に答える 2

3

試す

Poll.first.responses.each(&:destroy)

削除は、結合モデルの関連付けが。の場合にのみ機能し:belongs_toます。

于 2012-08-10T00:29:29.543 に答える
0

destroy_allを使用する必要があります。これは、リレーションで最適に機能します。

@poll.responses.destroy_all
于 2012-08-10T00:20:47.377 に答える