なるようになるブログ

読書感想文かrailsについてかrubyについてか

rails commit log流し読み(2014/04/29)

2014/04/29分のコミットです。

ActiveRecord::Associations::CollectionAssociation#delete_allメソッドのリファクタリングに関するコミットが多くありました。

ActiveRecord::Associations::CollectionAssociation#delete_allに:nullifyを指定した場合に発行されるSQLが少し変わっています。


Added a missing end

rails guideの修正。active_support_instrumentationのページで、endが足りてなかったのを追加しています。


write a new method to be accessed from delete_all

ActiveRecord::Associations::CollectionAssociation#delete_allメソッドのリファクタリング

delete_allメソッドは引数により処理の分岐が起こるのですが、その分岐処理をdeleteメソッドで行っていたのを、その処理をdelete_all_with_dependencyに切り出して、delete_allメソッドからはdelete_all_with_dependencyメソッドを呼び出すように修正しています。

知らなかったのですが、delete_allメソッドに引数指定出来るんですねえ。

User.todos.delete_all(:nullify)
# UPDATE "todos" SET "user_id" = NULL WHERE "todos"."user_id" = ? AND "todos"."id" IN (10, 11)  [["user_id", 2]]

User.todos.delete_all(:delete_all)
# DELETE FROM "todos" WHERE "todos"."user_id" = ? AND "todos"."id" IN (12, 13, 14, 15, 16, 17)  [["user_id", 2]]

nullifyを引数に指定した場合、DELETEではなく、UPDATEが発行されるんですね。


remove unnecessary code from delete method

上記コミットでdelete_all_with_dependencyを作成した事により、deleteメソッドから不要になった処理を削除しています。


simplify the delete all w/ dependency method

delete_all_with_dependencyリファクタリングしています。不要な判定処理を削除しています。


[ci skip] updating active_record/associations to demonstrate where conflict with eager loading.

ActiveRecord::Associationsのコメントの修正。LEFT OUTER JOINを使用した場合の例を追加しています。

# If you want to load all posts (including posts with no approved comments) then write
# your own LEFT OUTER JOIN query using ON
#
#   Post.joins('LEFT OUTER JOIN comments ON comments.post_id = posts.id AND comments.approved = true')
#

flip conditional in delete_all to handle nullify better

上記でコミットされたdelete_all_with_dependencyリファクタリングです。

:nullifyを指定されている際に、associationsがload済みの場合、発行されるSQLにin statementが含まれるのですが、load済みの場合でも、 in statementが含まれないよう修正しています。

# before
UPDATE "categorizations" SET "category_id" = NULL WHERE "categorizations"."category_id" = 1 AND "categorizations"."id" IN (1, 2)

# after
UPDATE "categorizations" SET "category_id" = NULL WHERE "categorizations"."category_id" = 1

そもそもassociationsがloadされてない場合は、afterの方のSQLが発行されていたので、処理が統一された、と言えない事も無いのですが…。少し違和感が。


rewrite test to correctly test clear method

associations/callbacks_test.rbの修正です。テストメソッド名を修正しています。


clear shouldnt fire callbacks so remove order test

こちらはactiverecord/test/cases/associations/has_many_through_associations_test.rbの修正。不要なコードを削除しています。


add test to check that loaded and non laoded are the same

こちらもactiverecord/test/cases/associations/has_many_through_associations_test.rbの修正。

associationsがload済み/済みでないにかかわらず、同じSQLが発行されるようになったので、そのチェック用のテストコード追加しています。


Update the Getting Started tutorial

rails guideの修正。 Getting Startedを修正しています。

コードサンプルや説明が不足していた部分が大分追加されています。