【问题标题】:Create join model record with accepts_nested_attributes_for使用 Accepts_nested_attributes_for 创建连接模型记录
【发布时间】:2014-04-11 10:26:23
【问题描述】:

我有以下型号

class User < ActiveRecord::Base
   has_many :project_users, dependent: :destroy
   has_many :projects, through: :project_users
end

class ProjectUser < ActiveRecord::Base
   belongs_to :user
   belongs_to :project
   has_many :participants
   has_many :tasks, through: :participants
end

class Task < ActiveRecord::Base
   belongs_to :project
   has_many :participants
   has_many :project_users, through: :participants
   accepts_nested_attributes_for :participants
end

class Participant < ActiveRecord::Base
   belongs_to :project_user
   belongs_to :task
 end

所以流程应该是这样的: 1.用户创建项目 2. 用户通过连接模型 ProjectUser 将用户添加到项目中 3. 用户为该项目创建一个任务,并从 ProjectUsers 中选择那些将参与该任务的用户。所以我放了一个accepts_nested_attributes_for方法并尝试构建嵌套表单。

在我的控制器中:

 def new
    @task = Task.new
    @task.participants.build
 end

 def task_params
    params.require(:task).permit(:project_id, :project_phase_id, :priority_id, :title,     :due_date, :estimation, :responsible_id, :description, :participant_ids => [])#, :participants_attributes => [:project_user_id, :task_id])
 end

participants_attributes 被评论

在我看来:

= f.association :participants, as: :select

生成的实际 HTML:

 <input name="task[participant_ids][]" type="hidden" value="">
 <select class="select optional form-control" id="task_participant_ids" multiple="multiple" name="task[participant_ids][]">
   <option value="57">AlexandeR MazeiN</option>
   <option value="59">Firenze</option>
   <option value="58">Vasily Strekotkin</option>
 </select>

我通过 ajax 添加选项,value = ProjectUser.id 我必须这样做,因为除非选择任务的具体项目,否则我不知道会有哪些 ProjectUser。

我遇到的错误:

在 2014-04-11 13:18:24 +0300 为 127.0.0.1 开始 POST "/tasks" 用户负载 (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 6 ORDER BY "users"."id" ASC LIMIT 1 TasksController#create 作为 HTML 处理 参数:{"utf8"=>"✓", "authenticity_token"=>"aXuk9ZuDvFZce+sbIQLRhWZlVjitMvySaJ7CwWfdmaQ=", "task"=>{"project_id"=>"20", "priority_id"=>"4", "project_phase_id "=>"40", "title"=>"Skepta", "due_date"=>"", "estimation"=>"8", "responsible_id"=>"6", "participant_ids"=>["" , "57", "58"], "description"=>""}, "commit"=>"创建任务"} 团队负载 (0.4ms) SELECT "teams".* FROM "teams" WHERE "teams"."id" = $1 LIMIT 1 [["id", 3]] 参与者负载 (0.5ms) SELECT "participants".* FROM "participants" WHERE "participants"."id" IN (57, 58) 完成 404 Not Found in 7ms ActiveRecord::RecordNotFound - 找不到 ID 为 (57, 58) 的所有参与者(找到 0 个结果,但正在寻找 2 个):

【问题讨论】:

  • 请添加错误回溯。
  • 什么是 57、58 和 59 的 id?即哪个班?
  • 57、58、59是Projectuser的id

标签: ruby-on-rails nested-attributes has-and-belongs-to-many


【解决方案1】:

您的param 哈希具有来自ProjectUser 的ID 作为participant_ids,因此当它查询数据库时,它正在寻找具有这些ID 的Participant 模型。您需要在参与者列表中将这些设置为project_user_id,如下所示:

participants: [ { project_user_id: 57 }, { project_user_id: 58 } ]

我对 build 不是很熟悉,但是这些方面的东西应该允许 AR 正确地构建关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多