{"id":1171,"date":"2011-08-11T14:17:15","date_gmt":"2011-08-11T05:17:15","guid":{"rendered":"http:\/\/ecpplus.net\/weblog\/?p=1171"},"modified":"2020-06-15T22:05:56","modified_gmt":"2020-06-15T13:05:56","slug":"unit-test-%e3%81%ae-fixtures-%e3%81%a7%e9%96%a2%e9%80%a3%e3%81%8c%e3%81%aa%e3%81%84%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae-primary-key-%e3%82%92%e5%8f%96%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/unit-test-%e3%81%ae-fixtures-%e3%81%a7%e9%96%a2%e9%80%a3%e3%81%8c%e3%81%aa%e3%81%84%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e3%81%ae-primary-key-%e3%82%92%e5%8f%96%e3%82%8b%e6%96%b9%e6%b3%95\/","title":{"rendered":"unit test \u306e fixtures \u3067\u95a2\u9023\u304c\u306a\u3044\u30c6\u30fc\u30d6\u30eb\u306e primary key \u3092\u53d6\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>Rails \u306e unit test \u3092\u66f8\u3044\u3066\u3044\u3066\u3001\u95a2\u9023\u304c\u306a\u3044\u30c6\u30fc\u30d6\u30eb\u306e primary key \u304c\u77e5\u308a\u305f\u3044\u72b6\u6cc1\u304c\u3042\u3063\u305f\u3002<\/p>\n<p>\u95a2\u9023\u304c\u3042\u308b\u30c6\u30fc\u30d6\u30eb\u3060\u3068\u3001<\/p>\n<pre lang=\"ruby\">\nclass User < ActiveRecord::Base\nhas_many :posts\nend\nclass Post < ActiveRecord::Base\nbelongs_to :user\nend\n<\/pre>\n<p>\u307f\u305f\u3044\u306a\u3068\u304d\u306b<\/p>\n<p>test\/fixtures\/users.yml<\/p>\n<pre lang=\"yml\">\nchihaya:\nname: \u5343\u65e9\n<\/pre>\n<p>test\/fixtures\/posts.yml<\/p>\n<pre lang=\"yml\">\nfirst:\nuser: chihaya\ntitle: \u304f\u3063\u2026\uff01\nbody: \u5148\u65e5\u30d5\u30a7\u30b9\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u2026\n<\/pre>\n<p>\u307f\u305f\u3044\u306a\u611f\u3058\u306b\u3059\u308b\u3068\u95a2\u9023\u304c\u4f5c\u308c\u308b\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u5358\u7d14\u306a\u30ed\u30b0\u307f\u305f\u3044\u306a\u3082\u306e\u3067\u3001\u8272\u3093\u306a\u30c6\u30fc\u30d6\u30eb\u306e id \u3092\u3001target_id \u307f\u305f\u3044\u306a\u3068\u3053\u308d\u306b\u3057\u307e\u3063\u3066\u3001\u30af\u30e9\u30b9\u540d\u3068\u5408\u308f\u305b\u3066\u30e6\u30cb\u30fc\u30af\u306b\u306a\u308b\u3088\u3046\u306b\u7ba1\u7406\u3057\u3066\u305f\u3002\u6570\u304c\u591a\u304b\u3063\u305f\u306e\u3068\u95a2\u9023\u4ed8\u3051\u3066\u53d6\u308b\u5fc5\u8981\u304c\u7121\u304b\u3063\u305f\u306e\u3067\u3001\u95a2\u9023\u3092\u4f5c\u3063\u3066\u306a\u304b\u3063\u305f\u3093\u3060\u3051\u308c\u3069\u3082\u3001fixture \u3092\u66f8\u304f\u6642\u306b\u3001\u5916\u90e8\u306e id \u306e\u53d6\u5f97\u65b9\u6cd5\u306b\u8ff7\u3063\u305f\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u5148\u307b\u3069\u306e\u4f8b\u3067\u3001has_many, belongs_to \u304c\u7121\u304b\u3063\u305f\u6642\u306b\u3069\u3046\u3059\u308b\u304b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<br \/>\nusers.yml \u306b\u3001 id:1 \u3068\u304b\u66f8\u304f\u3068\u3044\u3046\u306e\u30821\u3064\u306e\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>id \u3092\u6307\u5b9a\u305b\u305a\u306b rake fixtures:load \u3063\u3066\u3059\u308b\u3068\u3001id \u304c\u7d50\u69cb\u5927\u304d\u306a\u6570\u5b57\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u3042\u308c\u306f\u30e9\u30d9\u30eb\u306e\u6587\u5b57\u5217\u306b\u3088\u3063\u3066\u8a08\u7b97\u3055\u308c\u305f\u5024\u307f\u305f\u3044\u3067\u3059\u306d\u3002<\/p>\n<p><a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveRecord\/Fixtures.html#method-c-identify\" title=\"Fixtures#identify\" target=\"_blank\" rel=\"noopener noreferrer\">ActiveRecord::Fixtures.identify(label)<\/a> \u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3067\u8a08\u7b97\u3055\u308c\u3066\u3044\u3066\u3001\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u4f9d\u5b58\u3067\u3001\u540c\u3058\u6587\u5b57\u5217\u3092\u4e0e\u3048\u308c\u3070\u540c\u3058\u5024\u304c\u8fd4\u3063\u3066\u304f\u308b\u4ed5\u7d44\u307f\u306b\u306a\u3063\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u305d\u3046\u3059\u308b\u3068\u3001\u5148\u307b\u3069\u306e\u4f8b\u3060\u3068<\/p>\n<p>test\/fixtures\/posts.yml<\/p>\n<pre lang=\"yml\">\nfirst:\nuser_id: <%= ActiveRecord::Fixtures.identify(:chihaya) %>\ntitle: \u304f\u3063\u2026\uff01\nbody: \u5148\u65e5\u30d5\u30a7\u30b9\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u2026\n<\/pre>\n<p>\u3068\u3059\u308b\u3068\u3001\u5e38\u306b\u5343\u65e9\u306e id \u304c\u53d6\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002ActiveRecord::Fixtures.identify(:chihaya) \u306e\u5024\u306f\u3001\u898b\u3066\u5206\u304b\u308b\u3088\u3046\u306b\u30c6\u30fc\u30d6\u30eb\u306b\u4f9d\u5b58\u3059\u308b\u3053\u3068\u306f\u306a\u304f\u3001\u5225\u30c6\u30fc\u30d6\u30eb\u3067 chihaya \u3068\u3044\u3046\u30e9\u30d9\u30eb\u3092\u4f7f\u3046\u3068\u3001\u540c\u3058 id \u306b\u306a\u308a\u307e\u3059\u3002ActiveRecord::Fixtures \u304c\u898b\u3064\u304b\u3089\u306a\u3044\u3068\u304d\u306f\u3001 require 'active_record\/fixtures' \u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u306a\u3093\u3060\u308d\u3046\u306a\u30fc\u3068\u601d\u3063\u3066\u305f\u5de8\u5927\u306a\u6574\u6570\u306e id \u306e\u8a08\u7b97\u65b9\u6cd5\u304c\u308f\u304b\u3063\u3066\u3059\u3063\u304d\u308a\u3057\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails \u306e unit test \u3092\u66f8\u3044\u3066\u3044\u3066\u3001\u95a2\u9023\u304c\u306a\u3044\u30c6\u30fc\u30d6\u30eb\u306e primary key \u304c\u77e5\u308a\u305f\u3044\u72b6\u6cc1\u304c\u3042\u3063\u305f\u3002 \u95a2\u9023\u304c\u3042\u308b\u30c6\u30fc\u30d6\u30eb\u3060\u3068\u3001 class User < ActiveRecord::Base has_ [&hellip;]\n<\/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,8],"class_list":["post-1171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-rails","tag-ruby"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1171","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=1171"}],"version-history":[{"count":10,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1171\/revisions"}],"predecessor-version":[{"id":2531,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/1171\/revisions\/2531"}],"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=1171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=1171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=1171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}