我在 Rails 应用程序中使用acts-as-taggable-on,每次创建新照片(例如)时,我都会在“标记”表中得到重复的行。
我的模型类看起来像:
class User < ActiveRecord::Base
acts_as_tagger
...
end
和
class Photo < ActiveRecord::Base
acts_as_taggable_on :tags
...
end
并在我的 photos_controller 的创建操作中
def create
@user = current_user
...
@user.tag(@photo, :with => params[:photo][:tag_list], :on => :tags)
...
end
奇怪的是,我在 'taggings' 表中得到重复的行,其中第一行的 'tagger_id' 和 'tagger_type' 设置为 NULL,而重复的行具有正确的值。
我的 Gemfile 看起来像这样
gem 'rails', '3.2.8'
gem 'acts-as-taggable-on', '~> 2.3.1'
有没有人见过这种行为?这是我的配置问题吗?
更新:查看控制台,我可以清楚地看到正在执行的两个事务,第一个事务是这样的:
SQL (0.6ms) INSERT INTO "taggings" ("context", "created_at", "tag_id",
"taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES (?, ?, ?, ?, ?, ?, ?)
[["context", "tags"], ["created_at", Thu, 27 Sep 2012 21:49:22 UTC +00:00], ["tag_id",
12], ["taggable_id", 10], ["taggable_type", "Photo"],
["tagger_id", nil], ["tagger_type", nil]]
很明显,tagger_id 和tagger_type 都设置为null。
这是一个完整的控制台输出,我已将各行分开以帮助阅读它。您会注意到两个单独的事务,在第一个事务中插入了 NULL 值,而在第二个事务结束时,您将看到正确的事务。
在 2012-09-28 07:39:58 +0200 开始 POST "/photo" for 127.0.0.1 由 PhotoController#create 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"IOmnfDpU7V7vYw3h6RXXzXPsXf/ B0fcVihXhb+S8JHU=", "photo"=>{"url"=>"www.another.com/photo.jpg", "title"=>"Another", "tag_list"=>"a_tag", "description" =>"", "private"=>"0"}, "commit"=>"添加照片"} 重定向到http://www.somedomain.com:3000/users/christiangiacomi
完成 302 Found in 414ms (ActiveRecord: 20.5ms)
用户负载 (0.3ms) SELECT "users".* FROM "users" WHERE "users"."username" = 'christiangiacomi' LIMIT 1 ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER在“tags”中加入“taggings”。“id”=“taggings”。“tag_id”其中“taggings”。“taggable_id”为空且“taggings”。“taggable_type”='照片'和(taggings.context = 'tags ' AND taggings.tagger_id 为 NULL)
(0.1ms) 开始交易
SQL (6.2ms) INSERT INTO "photos" ("created_at", "description", "favorite", "private", "title", "updated_at", "url", "user_id") VALUES (?, ?, ? , ?, ?, ?, ?, ?) [["created_at", Fri, 28 Sep 2012 05:39:59 UTC +00:00], ["description", ""], ["favorite", false] , ["private", false], ["title", "Another"], ["updated_at", Fri, 28 Sep 2012 05:39:59 UTC +00:00], ["url", "http:// /www.another.com/photo.jpg"], ["user_id", 1]]
ActsAsTaggableOn::Tag Load (3.2ms) SELECT "tags".* FROM "tags" WHERE (lower(name) = 'a_tag') ActsAsTaggableOn::Tag Exists (0.1ms) SELECT 1 AS one FROM "tags" WHERE "tags "."name" = 'a_tag' 限制 1
SQL (0.2ms) INSERT INTO "tags" ("name") VALUES (?) [["name", "a_tag"]]
ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 13 AND "taggings"."taggable_type" = 'Photo' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
ActsAsTaggableOn::Tagging Exists (0.2ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 16 AND "taggings"."taggable_type" = 'Photo' AND "taggings"."taggable_id" = 13 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL)限制 1
SQL (0.7ms) INSERT INTO "taggings" ("context", "created_at", "tag_id", "taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES (?, ?, ?, ?, ? , ?, ?) [["context", "tags"], ["created_at", Fri, 28 Sep 2012 05:39:59 UTC +00:00], ["tag_id", 16], ["taggable_id" , 13], ["taggable_type", "Photo"], ["tagger_id", nil], ["tagger_type", nil]]
(4.1ms) 提交事务
(0.1ms) 开始交易
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" WHERE (lower(name) = 'a_tag')
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 13 AND "taggings"."taggable_type" = 'Photo' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
CACHE (0.0ms) SELECT "tags".* FROM "tags" WHERE (lower(name) = 'a_tag')
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 13 AND "taggings"."taggable_type" = 'Photo' AND (taggings.context = 'tags' AND taggings.tagger_id = 1 AND taggings.tagger_type = 'User')
ActsAsTaggableOn::Tagging Exists (0.4ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 16 AND "taggings"."taggable_type" = 'Photo' AND "taggings"."taggable_id" = 13 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" = 1 AND "taggings"."tagger_type" = 'User') 限制 1
SQL (0.5ms) INSERT INTO "taggings" ("context", "created_at", "tag_id", "taggable_id", "taggable_type", "tagger_id", "tagger_type") VALUES (?, ?, ?, ?, ? , ?, ?) [["context", "tags"], ["created_at", Fri, 28 Sep 2012 05:39:59 UTC +00:00], ["tag_id", 16], ["taggable_id" , 13], ["taggable_type", "Photo"], ["tagger_id", 1], ["tagger_type", "User"]]
(2.4ms) 提交事务