I have this controller:
def put_tags
user_to_tag = current_user
user_to_tag.tag_list = tag_params
if user_to_tag.save
redirect_to root_path
else
redirect_to root_path
end
end
private
def tag_params
params.require(:user).permit(:tag_list)
end
However, it is not treating params[:tag_list] as string but hash and storing
params.require(:user).permit(:tag_list)
as value.
Below is the SQL transaction:
Started PATCH "/usertag/put_tags" for 127.0.0.1 at 2015-06-11 15:03:28 +0800
Processing by UsertagController#put_tags as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BmtevgH0g3b8jFxB0ojjPaYGRBtLg4y0gZ1WFjuFUg3btTcuMcv/Hb8ho7fyBlRaRnElMaXZn4iUjrfMSths2g==", "user"=>{"tag_list"=>"jaso , j,df"}, "commit"=>"submit"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
ActsAsTaggableOn::Tag Load (0.3ms) 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", 1], ["taggable_type", "User"]]
(0.1ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE ("users"."username" = 'jason' AND "users"."id" != 1) LIMIT 1
SQL (0.6ms) UPDATE "users" SET "updated_at" = $1 WHERE "users"."id" = $2 [["updated_at", "2015-06-11 07:03:28.550917"], ["id", 1]]
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('{"tag_list"=>"jaso') OR LOWER(name) = LOWER('j') OR LOWER(name) = LOWER('df"}'))
ActsAsTaggableOn::Tag Exists (0.5ms) SELECT 1 AS one FROM "tags" WHERE "tags"."name" = '{"tag_list"=>"jaso' LIMIT 1
SQL (0.4ms) INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id" [["name", "{\"tag_list\"=>\"jaso"]]
ActsAsTaggableOn::Tag Exists (0.9ms) SELECT 1 AS one FROM "tags" WHERE "tags"."name" = 'j' LIMIT 1
SQL (0.2ms) INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id" [["name", "j"]]
ActsAsTaggableOn::Tag Exists (0.3ms) SELECT 1 AS one FROM "tags" WHERE "tags"."name" = 'df"}' LIMIT 1
SQL (0.2ms) INSERT INTO "tags" ("name") VALUES ($1) RETURNING "id" [["name", "df\"}"]]
ActsAsTaggableOn::Tag Load (0.2ms) 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", 1], ["taggable_type", "User"]]
ActsAsTaggableOn::Tagging Load (0.3ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL AND "taggings"."context" = $3 AND "taggings"."tag_id" IN (9, 8) [["taggable_id", 1], ["taggable_type", "User"], ["context", "tags"]]
SQL (0.2ms) DELETE FROM "taggings" WHERE "taggings"."id" = $1 [["id", 9]]
ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT 1 [["id", 9]]
SQL (0.4ms) UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) - 1 WHERE "tags"."id" = $1 [["id", 9]]
SQL (0.4ms) DELETE FROM "taggings" WHERE "taggings"."id" = $1 [["id", 8]]
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT 1 [["id", 8]]
SQL (0.6ms) UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) - 1 WHERE "tags"."id" = $1 [["id", 8]]
ActsAsTaggableOn::Tagging Exists (1.1ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 10 AND "taggings"."taggable_type" = 'User' AND "taggings"."taggable_id" = 1 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
SQL (0.3ms) INSERT INTO "taggings" ("tag_id", "context", "taggable_id", "taggable_type", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["tag_id", 10], ["context", "tags"], ["taggable_id", 1], ["taggable_type", "User"], ["created_at", "2015-06-11 07:03:28.582666"]]
ActsAsTaggableOn::Tag Load (0.1ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT 1 [["id", 10]]
SQL (0.2ms) UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1 [["id", 10]]
ActsAsTaggableOn::Tagging Exists (0.4ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 11 AND "taggings"."taggable_type" = 'User' AND "taggings"."taggable_id" = 1 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
SQL (0.3ms) INSERT INTO "taggings" ("tag_id", "context", "taggable_id", "taggable_type", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["tag_id", 11], ["context", "tags"], ["taggable_id", 1], ["taggable_type", "User"], ["created_at", "2015-06-11 07:03:28.588126"]]
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT 1 [["id", 11]]
SQL (0.3ms) UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1 [["id", 11]]
ActsAsTaggableOn::Tagging Exists (0.9ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 12 AND "taggings"."taggable_type" = 'User' AND "taggings"."taggable_id" = 1 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
SQL (0.2ms) INSERT INTO "taggings" ("tag_id", "context", "taggable_id", "taggable_type", "created_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["tag_id", 12], ["context", "tags"], ["taggable_id", 1], ["taggable_type", "User"], ["created_at", "2015-06-11 07:03:28.597740"]]
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = $1 LIMIT 1 [["id", 12]]
SQL (0.2ms) UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = $1 [["id", 12]]
(7.7ms) COMMIT
So my problems are:
- Though I used direct assignment to update/remove tags, why there are so many SQL checks and insertions? How to reduce SQL queries?
- How to transform params[:tag_list] into strings so that I can add tags correctly? I tried params[:tag_list].to_s, but no luck.
Please give advice.