使用has_many复选框创建的重复连接表记录无法取消选中/销毁它们

时间:2014-10-30 04:50:00

标签: ruby-on-rails-4 checkbox associations has-many-through jointable

我正在使用复选框跟踪 HABTM Railscast#17 ,以使用连接表创建关联记录。

我终于得到了我的记录保存到联接表....但是我无法通过检查或取消选中一个框来控制它们....这意味着取消选中记录什么都不做并且保留记录使得另一个重复记录。

我有3张桌子。

 :project has_many :restrictions, :through => :project_restrictions

我希望能够做的是拥有一系列复选框,我可以检查这些复选框以创建:project_restrictions。 ..或者,如果我取消选中它们,则删除< strong>:project_restriction 记录。

现在它只是保存多个记录,如果我取消选中,则不会删除它们。

我已将所有逻辑放在ProjectsController中,并运行通过名为&#34; add_restrictions&#34; 的自定义PATCH方法添加:project_restrictions 的方法。这应该是POST吗?我无法通过添加关联记录或POSTING仅关联记录来判断我是否已修补项目。

我的联接表有一个:id ,没有:timestamps ....我不知道这是否重要....显然,我&# 39; m new。

我正在使用rails 4.

我的模特

  class Project < ActiveRecord::Base

  has_many :project_restrictions, dependent: :destroy
  has_many :restrictions, :through => :project_restrictions

  accepts_nested_attributes_for :project_restrictions, allow_destroy: true, :reject_if => :all_blank


 class Restriction < ActiveRecord::Base

  has_many :projects, :through => :project_restrictions
  has_many :project_restrictions, dependent: :destroy

 class ProjectRestriction< ActiveRecord::Base

  belongs_to :restriction
  belongs_to :project
 end

**我的PATCH控制器创建关联记录的方法**

 def add_restrictions
  @diet = Restriction.k1.order(:name) 
  @project = Project.find(params[:p]) 

  params[:project][:restriction_ids].reject!(&:empty?).each do |restriction| 

  @proj_rule = ProjectRestriction.create!(:project_id => @project.id, :restriction_id => restriction) 
  @proj_rule.save

  end
  respond_to do |format| 

 format.html { redirect_to t1s3_path(:p => @project.id ) , 
 notice: 'Success! You added restrictions!' } 
 format.json {} 

 end 
 end

我的表单

   <%= form_for @project, url: add_restrictions_path(:p => @project.id) do |f| %>

   <div class=" fields ">
       <%= hidden_field_tag "project[restriction_ids][]", nil %>
            <% @diet.each do |restriction| %>

              <%= check_box_tag  "project[restriction_ids][]", restriction.id,          
           @project.restriction_ids.include?(restriction.id), id: dom_id(restriction)%> 
           <%= label_tag dom_id(restriction), restriction.name %><br>

           <% end %>

         </div>
  <%= f.submit %>

更新

我已将模型更新为&#34; uniq和inverse_of ....但是我仍然创建重复记录并且无法通过取消选中复选框来销毁它们

  class Project < ActiveRecord::Base

  has_many :project_restrictions -> { uniq }, dependent: :destroy, inverse_of: :project
  has_many :restrictions, -> { uniq }, through: :project_restrictions

  accepts_nested_attributes_for :project_restrictions, allow_destroy: true, :reject_if => :all_blank


 class Restriction < ActiveRecord::Base

  has_many :projects,  -> { uniq }, through: :project_restrictions
  has_many :project_restrictions,   -> { uniq }, dependent: :destroy, inverse_of: :restriction

 class ProjectRestriction< ActiveRecord::Base

    belongs_to :restriction,  -> { uniq }, inverse_of: :project_restrictions
    belongs_to :project, -> { uniq }, inverse_of: :project_restrictions
 end

1 个答案:

答案 0 :(得分:0)

add_restrictions 方法中进行更改,然后尝试

def add_restrictions
  @diet = Restriction.k1.order(:name) 
  @project = Project.find(params[:p]) 

   params[:project][:restriction_ids].reject!(&:empty?).each do |restriction| 
       @projectrestrictions = ProjectRestriction.all
       @projectrestrictions.each do |prj|
         if prj.restriction_id == restriction
           ProjectRestriction.destroy_all(prj.id)
         else
            @proj_rule = ProjectRestriction.create!(:project_id => @project.id, :restriction_id => restriction) 
            @proj_rule.save
         end
      end
  respond_to do |format| 

 format.html { redirect_to t1s3_path(:p => @project.id ) , 
 notice: 'Success! You added restrictions!' } 
 format.json {} 

 end 
 end