ruby on rails 4 - Duplicate join table records being created with has_many checkboxes, can't uncheck/destroy them -



ruby on rails 4 - Duplicate join table records being created with has_many checkboxes, can't uncheck/destroy them -

i next habtm railscast #17 on using checkboxes create associated records using bring together table.

i got records save bring together table....but can't command them checking or unchecking box....meaning unchecking record nil , leaving record checked makes duplicate record.

i have 3 tables.

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

what i'd able have array of checkboxes can check create :project_restrictions...or, if uncheck them, remove :project_restriction record.

right keeps saving multiple records , not delete them if uncheck one.

i have set logic in projectscontroller , running method adding :project_restrictions through custom patch method called "add_restrictions". should post? can't figure out if i'm patching project adding associated records or posting associated records.

my bring together table has :id , no :timestamps .... don't know if matters....obviously, i'm new.

i using rails 4.

my models

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

**my patch controller method create associated records **

def add_restrictions @diet = restriction.k1.order(:name) @project = project.find(params[:p]) params[:project][:restriction_ids].reject!(&:empty?).each |restriction| @proj_rule = projectrestriction.create!(:project_id => @project.id, :restriction_id => restriction) @proj_rule.save end respond_to |format| format.html { redirect_to t1s3_path(:p => @project.id ) , notice: 'success! added restrictions!' } format.json {} end end

and form

<%= form_for @project, url: add_restrictions_path(:p => @project.id) |f| %> <div class=" fields "> <%= hidden_field_tag "project[restriction_ids][]", nil %> <% @diet.each |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 %>

update:

i have updated models have "uniq , inverse_of ....however i'm still creating duplicate records , unable destroy them unchecking checkbox

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

change in add_restrictions method , seek

def add_restrictions @diet = restriction.k1.order(:name) @project = project.find(params[:p]) params[:project][:restriction_ids].reject!(&:empty?).each |restriction| @projectrestrictions = projectrestriction.all @projectrestrictions.each |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 |format| format.html { redirect_to t1s3_path(:p => @project.id ) , notice: 'success! added restrictions!' } format.json {} end end

ruby-on-rails-4 checkbox associations has-many-through jointable

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -