ruby on rails - how to remove records from the joined table? -
please solve problem.
table posts
create_table "posts", force: :cascade |t| t.string "title" t.text "body" end table tags
create_table "tags", force: :cascade |t| t.string "tagname" end joined table
create_table "posts_tags", id: false, force: :cascade |t| t.integer "post_id" t.integer "tag_id" end model post
class post < activerecord::base has_and_belongs_to_many :tags end model tag
class tag < activerecord::base has_and_belongs_to_many :posts end i need remove of association table posts_tags.
i form set id tags:
[3, 18, 21] here controller handles set id_tags:
def update if @post.update_attributes(post_params) #add_new_tags(@post) p '------------------1' p params['delete_tags'] p '------------------2' destroy_tags(params['delete_tags'], @post) flash[:success] = t :post_updated redirect_to user_post_path(@user, @post) else flash[:error] = t :post_not_updated render 'edit' end end private def destroy_tags(tags,post) tags.each |tag| p '==================' p tag tag_del = post.tags.find_by_post_id(:post_id => post.id) if teg_del post.tags.delete(tag_del) end end end def post_params params.require(:post).permit(:delete_tags) end as result, following error message:
undefined method `find_by_post' #<tag::activerecord_associations_collectionproxy:0x007f097ed78ac8> the console displays following message:
processing postscontroller#update html parameters: {"utf8"=>"✓", "authenticity_token"=>"jfnofjvn2ddqiwidk/swerfkipfj9btydwjy4qfbfl8ghro7tnzhbq4mcw+udydn9atleamfcpdlmhbvhmsdhq==", "post"=>{"title"=>"corrupti.ggh", "body"=>"suscipit ut odit labore fugiat quia aliquam."}, "tagnames"=>"", "delete_tags"=>["3", "18", "21"], "commit"=>"Сохранить post", "locale"=>"ru", "user_id"=>"24", "id"=>"359"} post load (0.2ms) select "posts".* "posts" "posts"."id" = ? limit 1 [["id", 359]] user load (0.1ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 24]] user load (0.1ms) select "users".* "users" "users"."remember_token" = ? limit 1 [["remember_token", "15166203712e74cc4638f34991c141f85c04a0e0"]] cache (0.0ms) select "users".* "users" "users"."id" = ? limit 1 [["id", "24"]] (0.1ms) begin transaction (0.0ms) commit transaction "------------------1" ["3", "18", "21"] "------------------2" "==================" "3" completed 500 internal server error in 6ms (activerecord: 0.5ms)
since have join table, tags table doesn't have post_id, find_by won't work. isn't needed though because post.tags retrieve tags connected post anyway.
you lot of functionality handling habtm relationships , forms out of box rails using things collection_check_boxes don't think need destroy tags method unless i'm misunderstanding trying accomplish. might consider adding/removing tag associations posts this:
post - has_and_belongs_to_many :tags
tag - has_and_belongs_to_many :posts
post update form:
<legend>tags</legend> <div class="form-group"> <%= f.collection_check_boxes(:tag_ids, tag.all, :id, :name) %> </div> make sure allow :tag_ids => [] in controller's post_params
the allow update tags belonging post adding new tags checked , removing tags unchecked in form , can have normal create , update controller actions without need destroy_tags method.
Comments
Post a Comment