{"id":918,"date":"2010-02-23T17:31:43","date_gmt":"2010-02-23T08:31:43","guid":{"rendered":"http:\/\/ecpplus.net\/weblog\/?p=918"},"modified":"2020-06-15T22:06:51","modified_gmt":"2020-06-15T13:06:51","slug":"rails-%e3%81%ae-dynamic-scopes-%e3%81%a7%e3%81%ae-delete_all-%e3%81%af%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%82%92-null-%e3%81%ab%e3%81%99%e3%82%8b%e3%81%a0%e3%81%91","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/rails-%e3%81%ae-dynamic-scopes-%e3%81%a7%e3%81%ae-delete_all-%e3%81%af%e5%a4%96%e9%83%a8%e3%82%ad%e3%83%bc%e3%82%92-null-%e3%81%ab%e3%81%99%e3%82%8b%e3%81%a0%e3%81%91\/","title":{"rendered":"Rails \u306e Dynamic Scopes \u3067\u306e delete_all \u306f\u5916\u90e8\u30ad\u30fc\u3092 NULL \u306b\u3059\u308b\u3060\u3051"},"content":{"rendered":"<p>Shop has_many Items \u3068\u3044\u3046\u72b6\u6cc1\u306e\u3068\u304d\u3001\u3068\u3042\u308b shop \u304c\u6301\u3063\u3066\u3044\u308b items \u3092\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u53d6\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002\u305d\u308c\u306b\u5bfe\u3057\u3066\u3001\u6761\u4ef6\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u307e\u3059\u3002<\/p>\n<pre lang=\"ruby\">>> @shop.items \n=> [Item id:21 shop_id: 1, Item id: 22, shop_id: 1, ....]\n>> @shop.item.find_all_by_category(\"CPU\") \n=> [Item id:23 shop_id: 1, Item id: 26, shop_id: 1, ....]\n<\/pre>\n<p>\u305d\u3053\u3067\u3001\u540c\u69d8\u306b\u3068\u3042\u308b Shop \u306e Items \u3092\u5168\u90e8 delete_all \u306b\u3057\u3088\u3046\u3068\u3057\u305f\u306e\u3067\u3059\u304c\u3001DELETE \u3055\u308c\u308b\u306e\u3067\u306f\u306a\u304f\u3001\u5916\u90e8\u30ad\u30fc\u306bNULL\u304c\u30bb\u30c3\u30c8\u3055\u308c\u308b\u3068\u3044\u3046\u6319\u52d5\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre lang=\"ruby\">>> @shop.items.delete_all<\/pre>\n<p>\u3068\u3059\u308b\u3068<\/p>\n<pre lang=\"sql\">  Item Update (3.0ms)   UPDATE `items` SET shop_id = NULL WHERE (shop_id = 1 AND id IN (22,23,24,25,26)) <\/pre>\n<p>\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>Rails \u306e API \u3092\u898b\u308b\u3068<\/p>\n<pre lang=\"ruby\"># File vendor\/rails\/activerecord\/lib\/active_record\/base.rb, line 897\n897:       def delete_all(conditions = nil)\n898:         sql = \"DELETE FROM #{quoted_table_name} \"\n899:         add_conditions!(sql, conditions, scope(:find))\n900:         connection.delete(sql, \"#{name} Delete all\")\n901:       end<\/pre>\n<p>\u307f\u305f\u3044\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001Dynamic Scopes \u306f\u307e\u305f\u5225\u306e\u3068\u3053\u308d\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3063\u307d\u3044\u3067\u3059\u3002<\/p>\n<pre lang=\"ruby\">>> @shop.items.destroy_all<\/pre>\n<p>\u3068\u3057\u3066\u307f\u308b\u3068\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b DELETE \u306b\u306a\u308a\u307e\u3059\u304c\u3001SQL \u304c\u767a\u884c\u3055\u308c\u307e\u304f\u3063\u3066\u308b\u306e\u3067\u3060\u3081\u307d\u3044\u3067\u3059\u3002\u305d\u3082\u305d\u3082 destroy \u306f\u3001destroy \u3092\u30c8\u30ea\u30ac\u30fc\u306b\u3057\u3066\u3044\u308b\u5404\u7a2e\u51e6\u7406\u3092\u3057\u3066\u304b\u3089 DELETE \u304c\u884c\u308f\u308c\u308b\u306e\u3067\u3001\u305d\u3082\u305d\u3082 delete \u3068\u306f\u610f\u5473\u5408\u3044\u304c\u9055\u3046\u3068\u3044\u3046\u306e\u3067\u4ed5\u65b9\u306a\u3044\u3067\u3059\u3002<\/p>\n<pre lang=\"sql\">Item Destroy (3.0ms)   DELETE FROM `items` WHERE WHERE id = 21\nItem Destroy (3.0ms)   DELETE FROM `items` WHERE WHERE id = 22\nItem Destroy (3.0ms)   DELETE FROM `items` WHERE WHERE id = 23<\/pre>\n<p>\u3075\u3064\u3046\u306b Item.delete_all \u3092\u547c\u3076\u304b\u306a\u3041<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shop has_many Items \u3068\u3044\u3046\u72b6\u6cc1\u306e\u3068\u304d\u3001\u3068\u3042\u308b shop \u304c\u6301\u3063\u3066\u3044\u308b items \u3092\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u53d6\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002\u305d\u308c\u306b\u5bfe\u3057\u3066\u3001\u6761\u4ef6\u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3082\u51fa\u6765\u307e\u3059\u3002 >> @shop.items =>  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[10],"class_list":["post-918","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/comments?post=918"}],"version-history":[{"count":2,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/918\/revisions"}],"predecessor-version":[{"id":2533,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/918\/revisions\/2533"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media\/2521"}],"wp:attachment":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media?parent=918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}