簡體   English   中英

為什么在更新過程中丟失了作為可標記行為的標記?

[英]Why are acts-as-taggable-on tags lost during update?

我在Ruby on Rails應用程序中使用行為標簽功能,以允許用戶將標簽添加到他們的聯系人列表中(如標記的地址簿)。 使用標准的RESTful路徑contacts/newcontacts/[:id]contacts/:id/edit等,對用戶添加/更新帶有標簽的聯系人非常適合用戶。

我現在正在嘗試添加功能,以便用戶可以向聯系人發送電子郵件,要求他們確認並完成其詳細信息。 電子郵件請求包括一個指向不同控制器上的頁面的鏈接,該控制器顯示一個帶有所有聯系方式的表格(又稱“確認表格”),減去標簽(對用戶而言是個人的)。

電子郵件鏈接的格式為:

contact_confirmation/[:id]/edit?contact_id=[:contact_id]

其中[:id]是令牌,它可以一次性訪問聯系人以更新其詳細信息(這就像重置標准密碼一樣)。

確認表單會正確更新所有聯系方式,但是由於某些原因,標簽會丟失。 兩種情況下的params []都相同,並且update方法也相同。

查看日志,直到第一個COMMIT為止它們都是相同的。 然后,確認表單開始一個新過程(BEGIN)並刪除標簽:

(0.2ms)  BEGIN
CACHE ActsAsTaggableOn::Tag Load (0.0ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 9], ["taggable_type", "Contact"]]
CACHE ActsAsTaggableOn::Tag Load (0.0ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 9], ["taggable_type", "Contact"]]
CACHE ActsAsTaggableOn::Tag Load (0.0ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."tagger_id" = $3 AND "taggings"."tagger_type" = $4 AND "taggings"."context" = $5  [["taggable_id", 9], ["taggable_type", "Contact"], ["tagger_id", 1], ["tagger_type", "User"], ["context", "tags"]]
ActsAsTaggableOn::Tagging Load (0.6ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."tagger_type" = $3 AND "taggings"."tagger_id" = $4 AND "taggings"."tag_id" IN (5, 43, 44) AND "taggings"."context" = $5  [["taggable_id", 9], ["taggable_type", "Contact"], ["tagger_type", "User"], ["tagger_id", 1], ["context", "tags"]]
SQL (0.5ms)  DELETE FROM "taggings" WHERE "taggings"."id" = $1  [["id", 152]]
ActsAsTaggableOn::Tag Load (0.5ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
SQL (0.8ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) - 1 WHERE "tags"."id" = $1  [["id", 5]]
ActsAsTaggableOn::Tag Load (1.3ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
SQL (0.8ms)  DELETE FROM "taggings" WHERE "taggings"."id" = $1  [["id", 153]]
ActsAsTaggableOn::Tag Load (0.4ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 43], ["LIMIT", 1]]
SQL (0.5ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) - 1 WHERE "tags"."id" = $1  [["id", 43]]
ActsAsTaggableOn::Tag Load (0.4ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 43], ["LIMIT", 1]]
ActsAsTaggableOn::Tagging Load (0.3ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."tag_id" = $1  [["tag_id", 43]]
SQL (0.5ms)  DELETE FROM "tags" WHERE "tags"."id" = $1  [["id", 43]]
SQL (0.5ms)  DELETE FROM "taggings" WHERE "taggings"."id" = $1  [["id", 154]]
ActsAsTaggableOn::Tag Load (0.4ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 44], ["LIMIT", 1]]
SQL (0.9ms)  UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) - 1 WHERE "tags"."id" = $1  [["id", 44]]
ActsAsTaggableOn::Tag Load (0.4ms)  SELECT  "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT $2  [["id", 44], ["LIMIT", 1]]
ActsAsTaggableOn::Tagging Load (0.5ms)  SELECT "taggings".* FROM "taggings" WHERE "taggings"."tag_id" = $1  [["tag_id", 44]]
SQL (2.0ms)  DELETE FROM "tags" WHERE "tags"."id" = $1  [["id", 44]]
ActsAsTaggableOn::Tag Load (3.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."tagger_id" = $3 AND "taggings"."tagger_type" = $4 AND "taggings"."context" = $5  [["taggable_id", 9], ["taggable_type", "Contact"], ["tagger_id", 1], ["tagger_type", "User"], ["context", "tags"]]
(1.2ms)  COMMIT

誰能幫我弄清楚為什么會這樣嗎? 謝謝。

回答者: https : //stackoverflow.com/a/25018540/6569994

我必須添加if tag_list_changed? 聯系模型上set_tag_owner條件:

def set_tag_owner
  if tag_list_changed?
    set_owner_tag_list_on(account, :tags, tag_list)
    self.tag_list = nil
  end
end

我認為在正常情況下,每次都必須剝離標簽並重新添加標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM