has_many 的作用域依赖销毁:通过关系

问题描述

我有以下型号:

class User << ApplicationRecord
  has_many :profiles
  has_many :questions,through: :profiles
  has_many :answers,through: :profiles
end

class Question < ApplicationRecord
  has_many :answers
  has_many :profiles
  has_many :users,through: :profiles
end

class Answer < ApplicationRecord
  belongs_to :question
  has_many :profiles
  has_many :users,through: :profiles
end

class Profile < ApplicationRecord
  belongs_to :answer
  belongs_to :user
  belongs_to :question,optional: true
end

并且我使用标准的 rails 表单来使用所选答案更新 USER 模型,如下所示(在 HAML 中):

  = form_for @user do |f|
    - if @user.errors.any?
      .alert.alert-danger
        = @user.errors.full_messages

    = f.collection_check_Boxes :answer_ids,@question.answers.all,:id,:label do |b|
      - content_tag :div,class: "form-check" do 
        - b.label(class: "form-check-label") do 
          - b.check_Box(class: "form-check-input") + b.text

    = f.submit 'Submit',class: 'btn btn-primary mt-4'

这工作正常,但是,当我在更新期间保存此表单中的值时,rails 首先从 Profiles 模型中删除所有未回答的选项,然后保存所选的选项 - 不论问题。

>

我知道这是标准的 Rails 行为,但是我如何确定删除的范围?我希望这样,Rails 只会破坏 Profile 模型中与所选答案具有相同问题 ID 的答案。

这甚至可能吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)