无法修改关联'调查#答案'因为它经历了多个其他协会

时间:2015-05-23 22:36:09

标签: ruby-on-rails forms nested

我有一个嵌套表单来处理调查及其答案。但是当我加载表单时,我收到了一个奇怪的错误:

的ActiveRecord :: HasManyThroughNestedAssociationsAreReadonly

有什么想法吗?我不确定如何解决这些关联。

try{
     int kernelSize = 3; // Change
     System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
     Mat source = Imgcodecs.imread("grayScale2.jpg",  Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
     Mat destination = new Mat(source.rows(),source.cols(),source.type());  
     Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F){
        {
           put(0,0,-1);
           put(0,1,0);
           put(0,2,1);

           put(1,0-2);          
           put(1,1,0);
           put(1,2,2);

           put(2,0,-1);
           put(2,1,0);
           put(2,2,1); // Leave it this way - don't uncomment
        }
     };       

     Imgproc.filter2D(source, destination, -1, kernel);
     Imgcodecs.imwrite("robinsonMaskExample.jpg", destination);

  } catch (Exception e) {
     System.out.println("Error: " + e.getMessage());
  }

调查#新

<%= form_for @survey do |f| %>

...
<%= f.fields_for :answers do |builder| %>
  <%= builder.text_field :content, :class=>"form-control" %>
<% end %>

...

<% end %>

Survey.rb

  def new
    @survey = Survey.new
    @template = Template.find(params[:template_id])
    @patient = Patient.find(params[:patient_id])
    @survey.answers.build
  end

Template.rb

class Survey < ActiveRecord::Base
      belongs_to :template
      has_many :questions, :through=> :template
      has_many :answers, :through=> :questions
      accepts_nested_attributes_for :answers
    end

Question.rb

class Template < ActiveRecord::Base
    belongs_to :survey
    has_many :questions
end

Answer.rb

class Question < ActiveRecord::Base
  belongs_to :template
  has_many :answers
end

1 个答案:

答案 0 :(得分:1)

您错过了Survey.rb中的第has_many :templates行。 您还必须在中指定:templates(复数型号名称) 同一档案:

has_many :questions, :through=> :templates

所以最后的变体是:

class Survey < ActiveRecord::Base
      has_many :templates
      has_many :questions, :through=> :templates
      has_many :answers, :through=> :questions
      accepts_nested_attributes_for :answers
    end

同样,您的模型surveyanswer相关联,您无需在控制器中获取模板:

def new
    @survey = Survey.new
    @patient = Patient.find(params[:patient_id])
    @survey.answers.build
end