我正在Ruby on Rails应用程序中使用行为标记功能,以允许用户将标记添加到他们的联系人列表中(如标记的地址簿)。使用标准RESTful路径contacts/new
,contacts/[:id]
,contacts/:id/edit
等为用户添加/更新带有标签的联系人非常适合
我现在正在尝试添加功能,以便用户可以向联系人发送电子邮件,要求他们确认并完成其详细信息。电子邮件请求包括指向不同控制器上构建的页面的链接,该页面显示带有所有联系方式的表格(又称“确认表格”),减去标签(对用户而言是个人的)。
电子邮件链接的格式为:
contact_confirmation/[:id]/edit?contact_id=[:contact_id]
其中[:id]
是令牌,它使联系人可以一次性访问以更新其详细信息(这就像重置标准密码一样)。
确认表单会正确更新所有联系方式,但是由于某些原因,标签会丢失。两种情况下的params []都相同,并且更新方法也相同。
查看日志,直到第一个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
有人可以帮我弄清楚为什么会这样吗?谢谢。
答案 0 :(得分:0)
回答者: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
我认为以正常的形式,每次都必须剥离标签并重新添加标签。