我的模型架构:
投票
has_many :questions
has_many :responses :through => :questions
问题
belongs_to :poll
has_many :responses
回应
belongs_to :question
问题当我尝试运行@poll.responses.delete_all
或clear
或destroy_all
时我得到了这个错误:
ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection:无法修改关联“Poll#responses”,因为源反射类“Response”通过 :has_many 与“Question”关联。
更新:仍然不确定为什么会发生这种情况,但有一个解决方法:
@poll.responses.each(&:destroy)
最佳答案
尝试
Poll.first.responses.each(&:destroy)
仅当连接模型上的关联为 :belongs_to
时,删除才有效。
关于ruby-on-rails - 通过rails has_many删除记录:through,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11893794/