在不清除标签的行为上作为可标记的行为的奇怪行为

时间:2015-08-21 07:06:46

标签: ruby-on-rails ruby ruby-on-rails-4 acts-as-taggable-on

我有一个名为Recipe的模型,它使用acts-as-taggable-on gem为食谱添加标记。奇怪的行为是当我通过控制器编辑和更新配方时,配方标记添加而不是更新到正确的标记。报告了类似的Github issue,但没有人回复。我的act-as-taggable-on gem版本是3.2.6。

例如,我的食谱有三个标记:fruitfruitvegetablesfruit标记不相关,因此我在fruit输入字段中删除了这两个f.tag_list标记,仅包含vegetables

Processing by Users::RecipesController#update as JS
Parameters: {"utf8"=>"✓", "recipe"=>{"image_ids"=>"46746", "cover_image_id"=>"46746",
"name"=>"Cauliflower", "description"=>"this is the description", "preparation_time_hr"=>"0",
"preparation_time_min"=>"50", "servings"=>"4", 
"category_ids"=>"", "cuisine_ids"=>"", "tag_list"=>"vegetables", "ingredients"=>....}

用户/ recipes_controller.rb

def update
  if @recipe.update_attributes(recipe_params)
    if params[:publish_recipe] && params[:publish_recipe] == "true"
      update_status!(:publish!)
    end
    redirect_to user_recipes_url
  else
    respond_to do |format|
      format.html{ render "edit" }
      format.js { render partial: "shared/flash_message", locals: { flash_type: "error", flash_message: "#{@recipe.errors.full_messages.to_sentence}" } }
    end
  end
end

recipe_params包含tag_list作为允许参数。

def recipe_params
  params.required(:recipe).permit(:name, :category_ids, :cuisine_ids, :tag_list, :preparation_time_hr,
  :cover_image_id, :preparation_time_min, :servings, :description, :image_ids, :commentable,
  ingredients_attributes: [:id, :name, :_destroy],
  instructions_attributes: [:id, :text, :_destroy, image_attributes: [:id, :picture, :_destroy]]
) rescue {}
end

但最终旧的标记未被删除,新标记已添加到列表中:fruitfruitvegetablesvegetables

另一个奇怪的事情是,当我尝试查看配方的tag_list时,它是一个空数组。尝试编辑rails控制台中的tag_list,它添加了另一个标记,但当我r.reload tag_list时,它仍然是一个空数组。现在配方和标签之间的关系是这样的:

r = Recipe.find(20980)
[56] pry(main)> r.tag_list
 ActsAsTaggableOn::Tag Load (14.9ms)  SELECT `tags`.* FROM `tags` INNER JOIN `taggings` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 20980 AND `taggings`.`taggable_type` = 'Recipe' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
=> []
[57] pry(main)> r.tags
=> [#<ActsAsTaggableOn::Tag id: 2, name: "fruit", taggings_count: 1138, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:47:20", updated_at: "2015-07-25 15:47:53">,
 #<ActsAsTaggableOn::Tag id: 2, name: "fruit", taggings_count: 1138, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:47:20", updated_at: "2015-07-25 15:47:53">,
 #<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">,
 #<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">,
 #<ActsAsTaggableOn::Tag id: 531, name: "vegetables", taggings_count: 21, tagger_id: nil, tagger_type: nil, created_at: "2015-07-25 15:56:15", updated_at: "2015-07-25 15:56:16">]
[58] pry(main)> r.taggings
  ActsAsTaggableOn::Tagging Load (35.8ms)  SELECT `taggings`.* FROM `taggings`  WHERE `taggings`.`taggable_id` = 20980 AND `taggings`.`taggable_type` = 'Recipe'
=> [#<ActsAsTaggableOn::Tagging id: 20408, tag_id: 2, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 03:56:13">,
 #<ActsAsTaggableOn::Tagging id: 20409, tag_id: 2, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 03:56:14">,
 #<ActsAsTaggableOn::Tagging id: 20410, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:01:36">,
 #<ActsAsTaggableOn::Tagging id: 20411, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:47:38">,
 #<ActsAsTaggableOn::Tagging id: 20412, tag_id: 531, taggable_id: 20980, taggable_type: "Recipe", tagger_id: 200422, tagger_type: "User", context: "tags", created_at: "2015-08-21 04:53:38">]

tag_list数据库查询也很奇怪,为什么查询包含此taggings.tagger_id IS NULL

我还是这个宝石的新手,关于如何使用gem方法正确更新标记的任何想法?我希望我可以避免使用自己的代码更新标记以避免进一步的问题。请注意,我的Recipe模型是由paper_trail gem版本化的,我希望它与此问题无关。

2 个答案:

答案 0 :(得分:1)

由于没有来自互联网的解决方案,我只在Recipe上使用这个标签,现在我手动调用方法在模型保存后重新更新标签。

def update_tags(tag_list)
  self.taggings.destroy_all
  list = tag_list.split(",")
  list.each do |t|
    tag = Tag.find_or_create_by(name: t)
    self.taggings.create(tag_id: tag.id, context: "tags")
  end
  return self.taggings
end

我仍然认为这个问题有更好的解决方案,我愿意接受替代方案作为最佳答案。

答案 1 :(得分:1)

我无法在关于tags_list作为参数进入update_attributes方法时发生的事情的文档中看到任何内容,但在您的情况下,它似乎添加了标签没有删除现有标签。我在文档中注意到tag_list可以使用替换现有标记的=进行分配:

  

您还可以通过直接分配添加和删除标记。请注意,这将删除现有标签,因此请注意使用它。

@user.tag_list = "awesome, slick, hefty"
@user.save

所以你可以在你的更新方法中做到这一点

def update
  if @recipe.update_attributes(recipe_params)
    @recipe.tag_list = recipe_params["tag_list"]
    @recipe.save
    ...
  else
    ...
  end
end