lundi 2 février 2015

delete_all doesn't support distinct error when removing child records via unchecked checkbox rails

I have 2 models, Posts & Tags, and a join table called Taggings. I create Taggings by checking a checkbox in a Tag.all array. That works. My error happens when I try to remove them by unchecking a checkbox. Then I get this error:



delete_all doesn't support distinct


Class



class PostsController < ApplicationController

def add_taggings
@post = Post.find(params[:id])
@post.tag_ids = params[:post][:tag_ids]
@tagging = Tagging.new
if @project.update_attributes(project_params)
if @post.update_attributes(post_params)
redirect_to @Post
flash[:notice] = "Successfully Tagged

else
render :edit
end
end

def post_params
params.require(:post).permit( :id, :title, :img, :content, :author,
taggings_attributes:[:id, :post_id, :tag_id, {:tag_ids => []}, :post_id, :_destroy])


Models



class Post < ActiveRecord::Base
has_many :taggings

class Tag < ActiveRecord::Base
has_many :taggings

class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :post

<%= form_for @post %>
<%= hidden_field_tag "post[tag_ids][]", nil%>

<% Tag.all.each do |tag| %>
<%= check_box_tag "post[tag_ids][]", tag.id, @post.tag_ids.include?(tag.id), id: dom_id(tag) %>
<%= label_tag dom_id(tag), tag.name %><br/>
<% end %>

<%= submit_tag 'ADD TAGS' %>




Aucun commentaire:

Enregistrer un commentaire