1

節省的has_many協會我創建一個project_management應用

當經理,當時我也想救開發商創建一個項目, 所以,我創建了user_projects模型Rails的accepts_nested_attributes_for不是通過

但我不能夠創建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.用戶工程項目必須存在

+0

你可以張貼'user_project'模式? – thaleshcv

+0

旁註:你的'開發者'範圍不存在,因爲has_many覆蓋它。當你調用'User.developers'時,你可以想到這一點嗎?相反,刪除範圍並將has_many更改爲'has_many:developers, - > {where(:role =>「developer」)},class_name:User,foreign_key::manager_id'這裏的「scope」包含在關係 – engineersmnky

+0

@thaleshcv user_project模式發佈 – Akshay

回答

0

您應該在項目模型中接受users的屬性,而不是users_projects。這是可以工作的,因爲您已經設置了用戶和項目之間的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 
0

我想在你的許可證PARAMS這個問題,試圖改變它,如

user_projects_attributes: [:id] 
相關問題