volpe28v/kotaren — pull request #2 by kaznum (2012-01-09 08:23:35+00:00)
Title: Save comment by more sufficient way.

The way to save a comment has called three SQLs.
1. insert a record ( both user_id and tune_id are null )
2. update the record ( update with user_id, still tune_id is null )
3. update the record ( update with tune_id. finally the record is sufficient status. )

INSERT INTO "comments" ("created_at", "text", "tune_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?) [["created_at", Mon, 09 Jan 2012 16:40:52 JST +09:00], ["text", "hogehoge"], ["tune_id", nil], ["updated_at", Mon, 09 Jan 2012 16:40:52 JST +09:00], ["user_id", nil]]
UPDATE "comments" SET "user_id" = 1, "updated_at" = '2012-01-09 16:40:52.143598' WHERE "comments"."id" = 1
UPDATE "comments" SET "tune_id" = 1, "updated_at" = '2012-01-09 16:40:52.156357' WHERE "comments"."id" = 1

The first Comment.create is redundant insert and the record is in insufficient status
until the second save call because tune_id and user_id are not stored.

This commit call only one SQL to store a record with the sufficient record status.

INSERT INTO "comments" ("created_at", "text", "tune_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?) [["created_at", Mon, 09 Jan 2012 17:05:42 JST +09:00], ["text", "eeeeeee"], ["tune_id", 2], ["updated_at", Mon, 09 Jan 2012 17:05:42 JST +09:00], ["user_id", 1]]

Additionally, Now the save! is called instead of save (no !) because save status has not been checked and if it failed, the user should notice the failure.