【问题标题】:Rails accepts_nested_attributes_for not saving association on has_many throughRails 接受_nested_attributes_for 不通过 h​​as_many 保存关联
【发布时间】:2018-02-22 07:38:47
【问题描述】:

我正在创建一个 project_management 应用程序

当时经理创建项目时我也想节省开发人员, 所以为此,我创建了 user_projects 模型

但我无法创建 user_projects 记录

型号

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  scope :developers, ->{ where(:role => "developer") }

  has_many :developers, class_name: "User",
                        foreign_key: "manager_id"
  has_many :user_projects
  has_many :projects, through: :user_projects
  belongs_to :manager, class_name: "User", optional: true

  enum role: {developer: 0, manager: 1}

  def full_name
    [first_name, last_name].join(' ')
  end
end

class Project < ApplicationRecord
  has_many :user_projects
  has_many :users, through: :user_projects

  accepts_nested_attributes_for :user_projects, reject_if: ->(object){ object[:user_id].blank? }
end

class UserProject < ApplicationRecord
  belongs_to :user
  belongs_to :project
end

项目控制器

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  def new
    @project = Project.new
    @project.user_projects.build
  end

  def create
    @project = Project.new(project_params)

    respond_to do |format|
      if @project.save
        format.html { redirect_to @project, notice: 'Project was successfully created.' }
        format.json { render :show, status: :created, location: @project }
      else
        format.html { render :new }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

  private

    def project_params
      params.require(:project).permit(:project_name, :project_description, :user_projects_attributes => [:user_id => []])
    end
end

项目创建表单

<%= simple_form_for(@project, html: { class: 'form-horizontal' }) do |form| %>
  <%= form.input :project_name %>
  <%= form.input :project_description %>
  <%= form.simple_fields_for :user_projects do |user_project| %>
    <%= user_project.association :user, :collection => User.developers, :label_method => :full_name, :value_method => :id, input_html: { multiple: true }%>
  <% end %>  
  <%= form.button :submit%>
<% end %>

遇到错误

禁止保存此项目的错误:
1.用户项目用户必须存在
2.用户项目项目必须存在

【问题讨论】:

  • 你能发布user_project 模型吗?
  • 旁注:您的 developers 范围不存在,因为 has_many 正在覆盖它。当你打电话给User.developers时,你会这样想,你期待哪一个?而是删除范围并将您的 has_many 更改为 has_many :developers, -&gt;{ where(:role =&gt; "developer") }, class_name: User, foreign_key: :manager_id 这里“范围”包含在关系的定义中
  • @thaleshcv user_project 模式已发布

标签: ruby-on-rails ruby many-to-many rails-activerecord has-many-through


【解决方案1】:

您应该在项目模型中接受 users 的属性,而不是 users_projects 的属性。这可以工作,因为您已经在 User 和 Project 之间设置了 has_many_through 关联。

这是一个达到关键点的简化示例。

class User < ApplicationRecord
  has_many :user_projects
  has_many :projects, through: :user_projects
end

class Projects < ApplicationRecord
  has_many :user_projects
  has_many :users, through: :user_projects
  accepts_nested_attributes_for :users
end

【讨论】:

    【解决方案2】:

    我认为您的许可参数中存在问题,请尝试更改它,例如

    user_projects_attributes: [:id]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2013-11-24
      • 2011-03-04
      相关资源
      最近更新 更多