Rails在连接表中创建多个记录

时间:2021-07-07 23:07:03

I'm trying to create multiple records in my join table via a create action. Here are my associations.

我正在尝试通过创建操作在我的连接表中创建多个记录。以下是我的联系。

class User
  has_many :abilities
  has_many :skills, through: :abilities
end

class Job
  has_many :abilities
  has_many :skills, through: :abilities
end

class Ability
  belongs_to :job
  belongs_to :skill
end

I have a form to create a new Job. There is a select box to choose what skills are needed for the job (Skills are already created). In the create action of jobs_controller.rb, how do I create multiple Abilities? Here's what I have.

我有一份新工作的表格。有一个选择框来选择工作所需的技能(技能已经被创建)。在jobs_controller的创建操作中。rb,我如何创建多重能力?这就是我。

def create
  @job = Job.new(job_params)
  @abilities = @job.abilities.build(params[:job][:skills])
end

My params returns an array for skills.

我的params返回一个技能数组。

"job"=> {
  "name"=>"abc",
  "skills"=>["2", "5"]
}

Getting stuck on how to create two Ability records in the create action of my jobs_controller (and associating the gig_id to the gig being created).

陷入了如何在jobs_controller的创建操作中创建两个能力记录(并将gig_id与正在创建的gig关联)。

2 个解决方案

#1


1  

You can use the singular_collection_ids method for this:

您可以使用singular_collection_ids方法:

#app/controllers/jobs_controller.rb
class JobsController < ApplicationController
   def new
      @job = Job.new
      @skills = Skill.all
   end

   def create
      @job = Job.new job_params
      @job.save
   end

   private

   def job_params
      params.require(:job).permit(skill_ids: [])
   end
end

#app/views/jobs/new.html.erb
<%= form_for @job do |f| %>
   <%= f.collection_select :skill_ids, @skills, :id, :name %>
   <%= f.submit %>
<% end %>

Having said this, you've got a major structural issue. You need to replace User with Skill:

说到这里,你会遇到一个主要的结构性问题。您需要用技能替换用户:

#app/models/skill.rb
class Skill < ActiveRecord::Base
  has_many :abilities
  has_many :jobs, through: :abilities
end

#app/models/ability.rb
class Ability < ActiveRecord::Base
  belongs_to :job
  belongs_to :skill
end

#app/models/job.rb
class Job < ActiveRecord::Base
  has_many :abilities
  has_many :skills, through: :abilities
end

#2


1  

the most simpliest way to do that is send from form "skill_ids"

最简单的方法是从“skill_ids”形式发送

"job"=> {
         "name"=>"abc",
         "skill_ids"=>["", "2", "5"]
         }

in controller

在控制器

def create
  @job = Job.new(job_params)
  # ...
end

def job_params
  params.require(:job).permit([:name, skill_ids: []])
end

needed abilities will be created automatically

需要的技能将自动创建

#1


1  

You can use the singular_collection_ids method for this:

您可以使用singular_collection_ids方法:

#app/controllers/jobs_controller.rb
class JobsController < ApplicationController
   def new
      @job = Job.new
      @skills = Skill.all
   end

   def create
      @job = Job.new job_params
      @job.save
   end

   private

   def job_params
      params.require(:job).permit(skill_ids: [])
   end
end

#app/views/jobs/new.html.erb
<%= form_for @job do |f| %>
   <%= f.collection_select :skill_ids, @skills, :id, :name %>
   <%= f.submit %>
<% end %>

Having said this, you've got a major structural issue. You need to replace User with Skill:

说到这里,你会遇到一个主要的结构性问题。您需要用技能替换用户:

#app/models/skill.rb
class Skill < ActiveRecord::Base
  has_many :abilities
  has_many :jobs, through: :abilities
end

#app/models/ability.rb
class Ability < ActiveRecord::Base
  belongs_to :job
  belongs_to :skill
end

#app/models/job.rb
class Job < ActiveRecord::Base
  has_many :abilities
  has_many :skills, through: :abilities
end

#2


1  

the most simpliest way to do that is send from form "skill_ids"

最简单的方法是从“skill_ids”形式发送

"job"=> {
         "name"=>"abc",
         "skill_ids"=>["", "2", "5"]
         }

in controller

在控制器

def create
  @job = Job.new(job_params)
  # ...
end

def job_params
  params.require(:job).permit([:name, skill_ids: []])
end

needed abilities will be created automatically

需要的技能将自动创建