{"id":1243,"date":"2012-12-25T22:56:28","date_gmt":"2012-12-25T13:56:28","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=1243"},"modified":"2018-11-14T16:33:58","modified_gmt":"2018-11-14T07:33:58","slug":"arrayextract_options-%e3%81%a7-rails-api-%e3%81%ae%e3%82%88%e3%81%86%e3%81%ab%e6%9f%94%e8%bb%9f%e3%81%aa%e5%bc%95%e6%95%b0%e3%82%92%e5%8f%96%e3%82%8b%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2012\/12\/25\/arrayextract_options-%e3%81%a7-rails-api-%e3%81%ae%e3%82%88%e3%81%86%e3%81%ab%e6%9f%94%e8%bb%9f%e3%81%aa%e5%bc%95%e6%95%b0%e3%82%92%e5%8f%96%e3%82%8b%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92\/","title":{"rendered":"Array#extract_options! \u3067 Rails API \u306e\u3088\u3046\u306b\u67d4\u8edf\u306a\u5f15\u6570\u3092\u53d6\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3059\u308b"},"content":{"rendered":"<p>\u3053\u3093\u306b\u3061\u306f\u3001\u9234\u6728\u3067\u3059\u3002<\/p>\n<p>Rails \u304c\u63d0\u4f9b\u3059\u308b API \u306e\u7279\u5fb4\u3068\u3057\u3066\u3001\u5f15\u6570\u306e\u6307\u5b9a\u304c\u67d4\u8edf\u3067\u3042\u308b\u70b9\u304c\u6319\u3052\u3089\u308c\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u67d4\u8edf\u306a\u5f15\u6570\u306e\u6307\u5b9a<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u4e00\u62ec\u4ee3\u5165\u3092\u8a31\u53ef\u3059\u308b\u5c5e\u6027\u3092\u6307\u5b9a\u3059\u308b attr_accessible \u30e1\u30bd\u30c3\u30c9\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8272\u3005\u306a\u547c\u3073\u51fa\u3057\u65b9\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\"># \u4e00\u3064\u3060\u3051\u6307\u5b9a\u3059\u308b\u30d1\u30bf\u30fc\u30f3.\r\nattr_accessible :name\r\n\r\n# \u8907\u6570\u6307\u5b9a\u3059\u308b\u30d1\u30bf\u30fc\u30f3.\r\nattr_accessible :name, :email\r\n\r\n# \u8907\u6570\u6307\u5b9a\u3057\u3066, \u3055\u3089\u306b\u30cf\u30c3\u30b7\u30e5\u3067\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u30d1\u30bf\u30fc\u30f3.\r\nattr_accessible :name, :email, :status, :as =&gt; :admin<\/pre>\n<p>\u5f15\u6570\u306f\u4e00\u3064\u6307\u5b9a\u3057\u3066\u3082\u8907\u6570\u6307\u5b9a\u3057\u3066\u3082\u826f\u304f\u3001:as =&gt; :admin \u306e\u3088\u3046\u306b\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>attr_accessible \u4ee5\u5916\u306b\u3082\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u5b9a\u7fa9\u3067\u4f7f\u7528\u3059\u308b resources \u3084\u3001\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u3092\u884c\u306a\u3046 rescue_from \u306a\u3069\u3001\u591a\u304f\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u5f15\u6570\u3092\u67d4\u8edf\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u5f15\u6570\u3092\u67d4\u8edf\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u30e1\u30bd\u30c3\u30c9\u306e\u5b9a\u7fa9\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u53ef\u5909\u9577\u5f15\u6570\u3092\u53d6\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">def attr_accessible(*args)\r\ndef resources(*resources, &amp;block)\r\ndef rescue_from(*klasses, &amp;block)<\/pre>\n<p>\u4f7f\u3046\u5074\u304b\u3089\u3059\u308b\u3068\u5f15\u6570\u6307\u5b9a\u304c\u67d4\u8edf\u3067\u3042\u308b\u3068\u4fbf\u5229\u3067\u3059\u304c\u3001\u305d\u306e\u3088\u3046\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u66f8\u3044\u305f\u4eba\u306f\u53ef\u5909\u9577\u5f15\u6570\u3092\u9069\u5207\u306b\u51e6\u7406\u3059\u308b\u30b3\u30fc\u30c9\u3092\u30e1\u30bd\u30c3\u30c9\u306e\u5148\u982d\u3067\u4e00\u751f\u61f8\u547d\u66f8\u3044\u3066\u3044\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Array#extract_options!<\/h2>\n<p>Array#extract_options! \u3092\u4f7f\u7528\u3059\u308b\u3068 attr_accessible \u306a\u3069\u306e\u3088\u3046\u306b\u67d4\u8edf\u306a\u5f15\u6570\u6307\u5b9a\u304c\u3067\u304d\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u7c21\u5358\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>Array#extract_options! \u306f ActiveSupport \u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u307e\u3059\uff08activesupport-3.2.9 \u3067\u78ba\u8a8d\uff09\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">class Hash\r\n\u00a0 def extractable_options?\r\n\u00a0\u00a0\u00a0 instance_of?(Hash)\r\n\u00a0 end\r\nend\r\n\r\nclass Array\r\n  def extract_options!\r\n    if last.is_a?(Hash) &amp;&amp; last.extractable_options?\r\n      pop\r\n    else\r\n      {}\r\n    end\r\n  end\r\nend<\/pre>\n<p>\u4e2d\u8eab\u306f\u5358\u7d14\u3067\u3001\u914d\u5217\u306e\u6700\u5f8c\u306e\u8981\u7d20\u304c Hash \u304b\u3064 extractable_options? \u304c true \u306a\u3089\u3001\u914d\u5217\u306e\u6700\u5f8c\u306e\u8981\u7d20\uff08Hash\uff09\u3092\u53d6\u308a\u51fa\u3059\u3001\u305d\u3046\u3067\u306a\u3051\u308c\u3070\u7a7a\u306e Hash \u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001Array#extract_options! \u306f\u53ef\u5909\u9577\u5f15\u6570\u304b\u3089\u30cf\u30c3\u30b7\u30e5\u3067\u6307\u5b9a\u3055\u308c\u305f\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u53d6\u308a\u51fa\u3059\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<p>\u52d5\u4f5c\u3092\u78ba\u8a8d\u3059\u308b\u305f\u3081\u306b\u3001\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u3001\u8272\u3005\u306a\u30d1\u30bf\u30fc\u30f3\u3067\u5f15\u6570\u3092\u6e21\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">def func(*args)\r\n  options = args.extract_options!\r\n  p args\r\n  p options\r\nend<\/pre>\n<p>\u307e\u305a\u306f\u5f15\u6570\u3092\u6307\u5b9a\u3057\u306a\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&gt; func\r\n[]\r\n{}<\/pre>\n<p>\u5f15\u6570\u3092\u4e00\u3064\u3060\u3051\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&gt; func('Hello')\r\n[\"Hello\"]\r\n{}<\/pre>\n<p>\u5f15\u6570\u3092\u8907\u6570\u6307\u5b9a\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&gt; func(:one, :two, :three)\r\n[:one, :two, :three]\r\n{}<\/pre>\n<p>\u4eca\u5ea6\u306f\u30cf\u30c3\u30b7\u30e5\u3067\u30aa\u30d7\u30b7\u30e7\u30f3\u3082\u6307\u5b9a\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&gt; func(:one, :two, :three, :a =&gt; true, :b =&gt; true)\r\n[:one, :two, :three]\r\n{:a=&gt;true, :b=&gt;true}<\/pre>\n<p>\u30cf\u30c3\u30b7\u30e5\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3060\u3051\u6307\u5b9a\u3059\u308b\u3068\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true\">&gt; func(:a =&gt; true, :b =&gt; true)\r\n[]\r\n{:a=&gt;true, :b=&gt;true}<\/pre>\n<p>Array#extract_options! \u3092\u4f7f\u7528\u3059\u308b\u3068\u3001\u300c\u5f15\u6570\u306f\u3044\u304f\u3064\u3067\u3082\u6307\u5b9a\u53ef\u80fd\uff08\u53ef\u5909\u9577\uff09\u3001\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u3042\u308b\u6642\u306f\u30cf\u30c3\u30b7\u30e5\u3067\u6307\u5b9a\u3059\u308b\u300d\u3068\u3044\u3046\u30d1\u30bf\u30fc\u30f3\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u7c21\u5358\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>Array#extract_options! \u306e\u4f7f\u7528\u4f8b<\/h2>\n<p>\u5177\u4f53\u7684\u306a\u4f7f\u7528\u4f8b\u3068\u3044\u3046\u3053\u3068\u3067\u3001<a title=\"URL \u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u4e00\u90e8\u3092\u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050\" href=\"http:\/\/www.techscore.com\/blog\/2012\/12\/10\/url-%e3%83%91%e3%83%a9%e3%83%a1%e3%83%bc%e3%82%bf%e3%81%ae%e4%b8%80%e9%83%a8%e3%82%92%e9%81%b7%e7%a7%bb%e5%85%88%e3%81%ab%e5%bc%95%e3%81%8d%e7%b6%99%e3%81%90\/\">URL \u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u4e00\u90e8\u3092\u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050<\/a>\u3067\u3054\u7d39\u4ecb\u3057\u305f\u3001take_params \u3092\u518d\u63b2\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:ruby decode:true \">class ApplicationController &lt; ActionController::Base\r\n\r\n  #\r\n  # take_params \u304c params \u304b\u3089\u53d6\u308a\u51fa\u3059\u30ad\u30fc\u306e\u30c7\u30d5\u30a9\u30eb\u30c8.\r\n  #\r\n  def default_take_param_keys\r\n    %w(page per_page)\r\n  end\r\n\r\n  #\r\n  # \u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050\u30d1\u30e9\u30e1\u30fc\u30bf.\r\n  #\r\n  # ==== \u8a73\u7d30\r\n  # \u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30ea\u30f3\u30af\u6642\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u4f7f\u7528\u3055\u308c\u308b\u3053\u3068\u3092\u60f3\u5b9a\u3057\u3066\u3044\u308b.\r\n  #\r\n  # link_to('\u30e6\u30fc\u30b6\u4e00\u89a7', users_path(take_params))\r\n  # link_to('\u30e6\u30fc\u30b6\u8a73\u7d30', user_path(@user, take_params))\r\n  #\r\n  # ==== \u5f15\u6570\r\n  # \u547c\u3073\u51fa\u3057\u5f62\u5f0f\u306f\u4ee5\u4e0b\u306e\u901a\u308a.\r\n  #\r\n  # (1) take_params\r\n  # params \u304b\u3089 default_take_param_keys \u304c\u8fd4\u3059\u540d\u524d\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059.\r\n  #\r\n  # (2) take_params(:key1, :key2)\r\n  # params \u304b\u3089 :key1, :key2 \u3092\u53d6\u308a\u51fa\u3059.\r\n  #\r\n  # (3) take_params(:additional_key1 =&gt; 1, :additional_key2 =&gt; 2)\r\n  # params \u304b\u3089 default_take_param_keys \u304c\u8fd4\u3059\u540d\u524d\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059.\r\n  # \u305d\u306e\u7d50\u679c\u306b {:additional_key1 =&gt; 1, :additional_key2 =&gt; 2} \u3092\u30de\u30fc\u30b8\u3059\u308b.\r\n  #\r\n  # (4) take_params(:key1, :key2, :additional_key1 =&gt; 1, :additional_key2 =&gt; 2)\r\n  # params \u304b\u3089 :key1, :key2 \u3092\u53d6\u308a\u51fa\u3059.\r\n  # \u305d\u306e\u7d50\u679c\u306b {:additional_key1 =&gt; 1, :additional_key2 =&gt; 2} \u3092\u30de\u30fc\u30b8\u3059\u308b.\r\n  #\r\n  # ==== \u623b\u308a\u5024\r\n  # \u9077\u79fb\u5148\u306b\u5f15\u304d\u7d99\u3050\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4fdd\u6301\u3059\u308b Hash.\r\n  #\r\n  def take_params(*param_keys)\r\n    overwrites = param_keys.extract_options!\r\n    param_keys = default_take_param_keys if param_keys.blank?\r\n    params.dup.extract!(*param_keys).update(overwrites)\r\n  end\r\n  helper_method :take_params\r\n\r\nend<\/pre>\n<p>&nbsp;<\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>Array#extract_options! \u3092\u4f7f\u7528\u3059\u308b\u3068\u67d4\u8edf\u306a\u5f15\u6570\u6307\u5b9a\u304c\u53ef\u80fd\u306a\u30e1\u30bd\u30c3\u30c9\u3092\u7c21\u5358\u306b\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3057\u3001\u67d4\u8edf\u306a\u5f15\u6570\u6307\u5b9a\u304c\u3067\u304d\u308b\u3053\u3068\u3068\u5f15\u304d\u66ff\u3048\u306b\u3001\u30e1\u30bd\u30c3\u30c9\u304c\u53ef\u5909\u9577\u5f15\u6570\u3092\u53d6\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u70b9\u306f\u8981\u6ce8\u610f\u3067\u3059\u3002<\/p>\n<p>\u300cdef func(*args)\u300d\u3068\u3044\u3046\u30b3\u30fc\u30c9\u304b\u3089\u8aad\u307f\u53d6\u308c\u308b\u3053\u3068\u306f\u3001\u3042\u307e\u308a\u591a\u304f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u4eba\u304c\u56f0\u3089\u306a\u3044\u3088\u3046\u306b\u3001<\/p>\n<ul>\n<li>\u30c9\u30ad\u30e5\u30e1\u30f3\u30c6\u30fc\u30b7\u30e7\u30f3\u30b3\u30e1\u30f3\u30c8\u3092\u3057\u3063\u304b\u308a\u8a18\u8ff0\u3059\u308b\u3002<\/li>\n<li>\u300cdef func(values, options={})\u300d \u3067\u5341\u5206\u306a\u5834\u5408\u306b\u3001\u308f\u3056\u308f\u3056\u53ef\u5909\u9577\u5f15\u6570\u306b\u3057\u306a\u3044\u3002<\/li>\n<\/ul>\n<p>\u3068\u3044\u3063\u305f\u3068\u3053\u308d\u304c\u30dd\u30a4\u30f3\u30c8\u3060\u3068\u601d\u3044\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>Rails \u304c\u63d0\u4f9b\u3059\u308b API \u306e\u7279\u5fb4\u3068\u3057\u3066\u3001\u5f15\u6570\u306e\u6307\u5b9a\u304c\u67d4\u8edf\u3067\u3042\u308b\u70b9\u304c\u6319\u3052\u3089\u308c\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2012\/12\/25\/arrayextract_options-%e3%81%a7-rails-api-%e3%81%ae%e3%82%88%e3%81%86%e3%81%ab%e6%9f%94%e8%bb%9f%e3%81%aa%e5%bc%95%e6%95%b0%e3%82%92%e5%8f%96%e3%82%8b%e3%83%a1%e3%82%bd%e3%83%83%e3%83%89%e3%82%92\/\">\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":[18],"tags":[68,22],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1243"}],"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=1243"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1243\/revisions"}],"predecessor-version":[{"id":17998,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/1243\/revisions\/17998"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}