{"id":182,"date":"2015-07-11T00:04:17","date_gmt":"2015-07-10T15:04:17","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/07\/11\/rails_web_api_validation"},"modified":"2022-10-26T10:10:09","modified_gmt":"2022-10-26T01:10:09","slug":"rails_web_api_validation","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/07\/11\/rails_web_api_validation\/","title":{"rendered":"Ruby on Rails \u3067 Web API \u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3059\u308b"},"content":{"rendered":"
MMM\u306e\u524d\u7530\u3067\u3059\u3002 \u672c\u65e5\u306fRuby on Rails<\/strong> \u306e Web API<\/strong>\u3067\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3069\u306e\u3088\u3046\u306b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u3057\u3066\u3044\u308b\u304b\u3092\u7e8f\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n ruby version 2.2.2 \u4f8b\u3048\u3070\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u7121\u3057\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d7\u3051\u53d6\u3063\u305f\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u3088\u3063\u3066\u671f\u5f85\u3057\u306a\u3044\u7d50\u679c\u306b\u306a\u3063\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306fActiveRecord<\/strong>\u306e\u57fa\u672c\u7684\u306a\u691c\u7d22\u30d1\u30bf\u30fc\u30f3\u3067\u3059\u304c\u3001 \u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u3067\u306f\u3001\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u306f\u914d\u5217\u3067\u53d7\u3051\u53d6\u308a\u3001\u30ec\u30b9\u30dd\u30f3\u30b9\u4e00\u89a7\u3092\u8fd4\u3059\u3001\u3068\u3044\u3046\u51e6\u7406\u3092\u3057\u305f\u304b\u3063\u305f\u3068\u3057\u3066\u3082\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u914d\u5217\u3067\u30dd\u30b9\u30c8\u3057\u306a\u3044\u6642\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002 \u3053\u3046\u3044\u3063\u305f\u3053\u3068\u3092\u672a\u7136\u306b\u9632\u3050\u305f\u3081\u306b\u3001\u578b\u30c1\u30a7\u30c3\u30af\u306a\u3069\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u51e6\u7406\u304c\u5fc5\u8981\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002 \u524d\u7f6e\u304d\u306f\u3055\u3066\u304a\u304d\u3001\u5b9f\u969b\u306eAPI\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u3069\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3059\u308b\u306e\u304cRuby on Rails<\/strong>\u3068\u3057\u3066\u826f\u3044\u65b9\u6cd5\u306a\u306e\u304b\uff1f\u3068\u3044\u3046\u3053\u3068\u3092\u3001\u793e\u5185\u306e\u30b5\u30fc\u30d0\u30fc\u30c1\u30fc\u30e0\u3067\u8b70\u8ad6\u3057\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n \u73fe\u5728\u4f7f\u3063\u3066\u3044\u308b\u65b9\u6cd5\u306f\u3001API\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u5c02\u7528\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u3001\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3001\u3068\u3044\u3046\u5b9f\u88c5\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n \u30fb\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u591a\u3044\u6642\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30ed\u30b8\u30c3\u30af\u304c\u8907\u96d1\u306a\u6642\u306f\u3001\u4fdd\u5b58\u3084\u691c\u7d22\u3092\u3059\u308b\u30af\u30e9\u30b9\u3068\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u307f\u3092\u3059\u308b\u30af\u30e9\u30b9\u3092\u5206\u5272\u3059\u308b \u4e0a\u8a182\u3064\u306e\u30d1\u30bf\u30fc\u30f3\u306b\u5206\u3051\u3066\u5b9f\u88c5\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n \u4f8b\u3048\u3070\u672c\u3092\u4fdd\u5b58\u3059\u308b\u3001\u3068\u3044\u3046API\u3092\u4f5c\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n \u4fdd\u5b58 + \u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30af\u30e9\u30b9<\/strong><\/p>\n
\n\u65e9\u3044\u3082\u306e\u3067\u3001MMM\u306b\u5165\u793e\u3057\u3066\u304b\u30891\u5e74\u304c\u7d4c\u904e\u3057\u307e\u3057\u305f\u3002
\n\u632f\u308a\u8fd4\u3063\u3066\u307f\u308b\u3068\u3001\u3053\u306e1\u5e74\u306f\u307b\u307cRuby on Rails<\/strong>\u3067Web API<\/strong>\u3092\u4f5c\u3063\u3066\u3044\u307e\u3057\u305f\u3002
\n\u5e38\u306bRails<\/strong>\u306b\u89e6\u308c\u308b\u3053\u3068\u304c\u51fa\u6765\u3001\u975e\u5e38\u306b\u697d\u3057\u3044\u4e00\u5e74\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n
\nRuby on Rails version 4.2.2<\/p>\n\u4f55\u6545WebAPI\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u304c\u5fc5\u8981\u306a\u306e\u304b<\/h2>\n
Book.where(id: params[:book_id])<\/code><\/pre>\n
params[:book_id]<\/code>\u304c\u6587\u5b57\u5217\u3067\u3082\u6570\u5024\u3067\u3082\u914d\u5217\u3067\u3082\u691c\u7d22\u3057\u307e\u3059\u3002
\n\u578b\u3092\u53b3\u5bc6\u306b\u30c1\u30a7\u30c3\u30af\u3057\u306a\u304f\u3066\u3082\u52d5\u3044\u3066\u3057\u307e\u3046\u3001\u3068\u3044\u3046\u3068\u3053\u308d\u304cRuby\u306e\u3044\u3044\u3068\u3053\u308d\u3067\u3082\u3042\u308a\u3001\u60aa\u3044\u3068\u3053\u308d\u3067\u3059\u3088\u306d\u3002
\n\u578b\u306eError<\/strong>\u306b\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306f\u3001\u958b\u767a\u8005\u304c\u53b3\u5bc6\u306b\u30c1\u30a7\u30c3\u30af\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3001\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
\n\u3057\u304b\u3057\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306f\u5c11\u306a\u304f\u3068\u3082\u3001\uff11\u3064\u306f\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u53d6\u5f97\u51fa\u6765\u308b\u306e\u3067\u3001\u30a8\u30e9\u30fc\u3067\u306f\u306a\u3044\u3068\u52d8\u9055\u3044\u3057\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u3066\u3057\u307e\u3046\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n
\n\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306a\u3069\u306b\u914d\u5217\u3067\u30dd\u30b9\u30c8\u3057\u3066\u4e0b\u3055\u3044\u3068\u66f8\u3044\u3066\u3044\u308b\u306e\u3067\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u8cac\u4efb\u3067\u3059\u3088<\/strong>\u3001\u3068\u3044\u3046\u898b\u65b9\u3082\u3042\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u305d\u3082\u305d\u3082API\u3068\u3057\u3066\u3001\u671f\u5f85\u3057\u306a\u3044\u30d1\u30e9\u30e1\u30fc\u30bf\u3067\u53d7\u3051\u4ed8\u3051\u304c\u51fa\u6765\u3066\u3057\u307e\u3046\u3001\u3068\u3044\u3046\u3053\u3068\u304c\u304a\u304b\u3057\u3044\u306e\u3060\u3068\u601d\u3044\u307e\u3059\u3002
\nWebAPI<\/strong>\u306f\u3001\u69d8\u3005\u306a\u958b\u767a\u8005\u304c\u4f7f\u3046\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u306e\u3067\u3001\u958b\u767a\u8005\u306b\u3088\u3063\u3066\u30df\u30b9\u304c\u8d77\u3053\u3063\u305f\u308a\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002
\n\u7d50\u8ad6\u3068\u3057\u3066\u306f\u3001\u3084\u306f\u308a\u30b5\u30fc\u30d0\u30fc\u5074\u3067\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u8a18\u8f09\u901a\u308a\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53b3\u5bc6\u306b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u3001\u4e0d\u6b63\u306a\u6642\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u6559\u3048\u3066\u3042\u3052\u308b\u3001\u3068\u3044\u3046\u51e6\u7406\u3092\u3059\u308b\u3053\u3068\u304c\u4e00\u756a\u826f\u3044\u306e\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\u3069\u306e\u3088\u3046\u306b\u5b9f\u88c5\u3059\u308b\u304b<\/h2>\n
\n\u30fb\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u5c11\u306a\u3044\u6642\u306f\u4fdd\u5b58\u3084\u691c\u7d22\u3059\u308b\u30af\u30e9\u30b9\u3068\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3059\u308b\u30af\u30e9\u30b9\u3092\u4e00\u7dd2\u306b\u3059\u308b<\/p>\n\u30ea\u30af\u30a8\u30b9\u30c8\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u5c11\u306a\u3044\u6642<\/h3>\n
# Book\nclass BookForm\n include Virtus.model\n include ActiveModel::Model\n\n NATURAL_NUMBER = { only_integer: true, greater_than_or_equal_to: 0 }\n\n attribute :price, Integer\n attribute :author, String\n attribute :publisher, String\n attribute :store, Array\n\n validates :book_id, presence: true, numericality: NATURAL_NUMBER\n validates :author, presence: true\n validates :publisher, presence: true\n validates :store_id, array_with_integer: true, if: 'store.present?'\n\n def save!\n book = Book.new(price: price, author: author, publisher: publisher)\n book.stores = Store.where(id: sotore_id)\n book.save!\n end\nend<\/code><\/pre>\n