{"id":4046,"date":"2014-03-07T14:00:24","date_gmt":"2014-03-07T05:00:24","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=4046"},"modified":"2018-11-14T16:33:51","modified_gmt":"2018-11-14T07:33:51","slug":"rails-4-1-0-active-record-enums","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2014\/03\/07\/rails-4-1-0-active-record-enums\/","title":{"rendered":"Rails 4.1.0 \u306e ActiveRecord enums \u3092\u4f7f\u3063\u3066\u307f\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u5148\u65e5\u3001Rails 4.1.0.rc1 \u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u30ea\u30ea\u30fc\u30b9\u30ce\u30fc\u30c8\u3092\u30d1\u30e9\u30d1\u30e9\u3092\u898b\u3066\u3044\u305f\u3089 ActiveRecord enums \u3068\u3044\u3046\u65b0\u6a5f\u80fd\u304c\u3042\u3063\u305f\u306e\u3067\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>ActiveRecord enums \u3068\u306f<\/h2>\n<p>\u4f1a\u54e1\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u6301\u3059\u308b users \u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u3066\u3001\u4f1a\u54e1\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u4fdd\u6301\u3059\u308b status \u30ab\u30e9\u30e0\u3092\u7528\u610f\u3057\u3066\u3001\u305d\u306e\u5024\u304c 0 \u306a\u3089\u4eee\u767b\u9332\u30011 \u306a\u3089\u672c\u767b\u9332\u30012 \u306a\u3089\u9000\u4f1a\u6e08\u307f\u306b\u3057\u3088\u3046\u3001\u30fb\u30fb\u30fb\u3068\u3044\u3046\u3053\u3068\u3063\u3066\u826f\u304f\u3042\u308a\u307e\u3059\u3088\u306d\uff1f<\/p>\n<p>ActiveRecord enums \u306f\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3067\u3053\u3093\u306a\u98a8\u306b\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class User &lt; ActiveRecord::Base\r\n\r\n  # \u30b9\u30c6\u30fc\u30bf\u30b9: temporary (\u4eee\u767b\u9332), active (\u672c\u767b\u9332), resign (\u9000\u4f1a).\r\n  enum status: %i(temporary active resign)\r\n\r\nend<\/pre>\n<p>\u3053\u3046\u3057\u3066\u304a\u304f\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># status \u304c 1 (active) \u306a\u30e6\u30fc\u30b6\u3092\u53d6\u5f97\u3059\u308b.\r\nuser = User.active.first!\r\n\r\n# status \u306f 0 (temporary) \u3067\u306f\u306a\u3044.\r\nuser.temporary? # =&gt; false\r\n\r\n# status \u3092\u78ba\u8a8d.\r\nuser.status # =&gt; \"active\"\r\n\r\n# status \u3092 2 (resign) \u306b\u5909\u66f4!\r\nuser.resign!\r\n\r\n# status \u306e\u53d6\u308b\u5024\u3092\u78ba\u8a8d.\r\nUser.statuses # =&gt; {\"temporary\"=&gt;0, \"active\"=&gt;1, \"resign\"=&gt;2}<\/pre>\n<p>ActiveRecord enums \u3092\u4f7f\u3046\u3068\u81ea\u52d5\u7684\u306b scope \u304c\u5b9a\u7fa9\u3055\u308c\u305f\u308a\u3001\u5024\u3092\u5909\u66f4\u3059\u308b\u30e1\u30bd\u30c3\u30c9 (resign! \u306a\u3069) \u304c\u5b9a\u7fa9\u3055\u308c\u308b\u3088\u3046\u3067\u3059\u3002\u5217\u6319\u578b\u306e\u3088\u3046\u306a\u3053\u3068\u3092\u7c21\u5358\u306b\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>DB \u306b\u767b\u9332\u3055\u308c\u308b\u5024\u3092 0, 1, 2, ... \u4ee5\u5916\u306b\u3059\u308b<\/h2>\n<p>enum status: %i(temporary active resign) \u3068\u3044\u3046\u5177\u5408\u306b enum \u306e\u7b2c\u4e8c\u5f15\u6570\u306b\u914d\u5217\u3092\u6e21\u3057\u305f\u5834\u5408\u3001DB \u306b\u767b\u9332\u3055\u308c\u308b\u5024\u306f 0, 1, 2, ... \u306b\u306a\u308a\u307e\u3059\u3002\u4ed6\u306e\u5024\u306b\u8a2d\u5b9a\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b Hash \u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">class User &lt; ActiveRecord::Base\r\n\r\n  # \u30b9\u30c6\u30fc\u30bf\u30b9: temporary (\u4eee\u767b\u9332), active (\u672c\u767b\u9332), resign (\u9000\u4f1a).\r\n  enum status: {temporary: 10, active: 20, resign: 30}\r\n\r\nend<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001temporary \u306a\u3089 10, active \u306a\u3089 20, resign \u306a\u3089 30 \u304c DB \u306b\u767b\u9332\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>\u5148\u65e5\u3001Rails 4.1.0.rc1 \u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u30ea\u30ea\u30fc\u30b9\u30ce\u30fc\u30c8\u3092\u30d1\u30e9\u30d1\u30e9\u3092\u898b\u3066\u3044\u305f\u3089 ActiveRecord enums \u3068\u3044\u3046\u65b0\u6a5f\u80fd\u304c\u3042\u3063\u305f\u306e\u3067\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2014\/03\/07\/rails-4-1-0-active-record-enums\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[135,18],"tags":[22,134,24],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4046"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=4046"}],"version-history":[{"count":7,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4046\/revisions"}],"predecessor-version":[{"id":4065,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/4046\/revisions\/4065"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=4046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=4046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=4046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}