首页 > 解决方案 > 通过关联向has_many中的连接表添加一个额外的字段

问题描述

在我的应用程序中,我有一些工作可以在完成后进行审查。审查表预先填充了预定义的值。经理应选中她想要添加到工作的所有评论的复选框。

我有一个 has_many :through 与额外列的关联,这是连接表中的一个额外关联:

class Job < ApplicationRecord
  has_many :job_reviews, dependent: :destroy
  has_many :reviews, through: :job_reviews

  accepts_nested_attributes_for :job_reviews, allow_destroy: true, reject_if: :all_blank
end

class Review < ApplicationRecord
  has_many :job_reviews, dependent: :destroy
  has_many :jobs, through: :job_reviews
end

class JobReview < ApplicationRecord
  belongs_to :job
  belongs_to :review
  belongs_to :user
end

在我看来,我有这样一个表格:

  = simple_form_for @job do |f|
    = f.association :reviews, collection: Review.all, as: :check_boxes, include_hidden: false, label: false
    = f.input :user_id, input_html: {value: current_user.id}
    = f.button :submit, class: 'btn btn-success'

控制器看起来像这样:

  def job_params
    params.require(:job).permit(:user_id, review_ids: [])
  end

当我运行代码时,这些是正在处理的参数:

<ActionController::Parameters {"user_id"=>"dfd24578-5asa-4143-b209-d13cb419af30", "review_ids"=>["453852c5-45f0-4f67-a41c-e7e50dab711a", "a1303a62-fbef-5asa-95a0-a3ffa0b7616c"]} permitted: true>

控制器中的创建方法:

  def create   
    respond_to do |format|
      if @job.update(job_params)
        format.js
      else
        format.js { render :js=>"alert('#{@job.errors.full_messages }');" }
      end
    end
  end

这是我得到的错误:

INSERT INTO "job_reviews" ("uuid", "review_id", "job_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "uuid"  [["id", "dfd24578-5asa-42b8-807c-38be1036bcf5"], ["review_id", "a1303a62-fbef-5asa-95a0-a3ffa0b7616c"], ["job_id", "cb7eba6e-95a0-45f0-81f2-490d1c80ee07"], ["created_at", "2019-06-13 12:04:08.581557"], ["updated_at", "2019-06-13 12:04:08.581557"]]
   (0.2ms)  ROLLBACK
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR:  null value in column "user_id" violates not-null constraint

标签: ruby-on-railsruby

解决方案


首先:

  • 模型中有错误JobReview。替换belongs_to :job_reviewbelongs_to :review
  • job_review_params方法应重命名为,job_params因为它实际上是Job您分配参数的对象。

当您分配reviews给一个job传递review_ids参数时,Rails 会尝试自动创建job_reviews关联。它失败是因为 Rails 不能自动计算该user_id值并且它没有正确传递。

尽管您在表单中有参数,但它是作为的属性user_id传递的。jobRails 不知道如何处理它。

解决问题的方法之一是reviews手动job分配:

job_params[:review_ids].each do |review_id|
  @job.job_reviews.build(review_id: review_id, user_id: current_user.id)
end

@job.save

在这种情况下,您不必user_id通过表单发送,因为它在控制器中可用:

= simple_form_for @job do |f|
  = f.association :reviews, collection: Review.all, as: :check_boxes, include_hidden: false, label: false
  = f.button :submit, class: 'btn btn-success'

推荐阅读