{"id":2214,"date":"2016-12-29T07:50:49","date_gmt":"2016-12-28T22:50:49","guid":{"rendered":"http:\/\/codaholic.org\/?p=2214"},"modified":"2017-01-04T21:25:33","modified_gmt":"2017-01-04T12:25:33","slug":"rails-self-abstract_class-true-%e3%81%ae%e6%84%8f%e5%91%b3%e3%81%a8%e6%8c%99%e5%8b%95","status":"publish","type":"post","link":"https:\/\/codaholic.org\/?p=2214","title":{"rendered":"[Rails] self.abstract_class = true \u306e\u610f\u5473\u3068\u6319\u52d5"},"content":{"rendered":"<p>ActiveRecord::Base \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u3092\u30e2\u30c7\u30eb\u3068\u3057\u3066\u4f5c\u6210\u3059\u308b\u3068\u3001Rails \u306f\u305d\u306e\u30af\u30e9\u30b9\u540d\u306b\u5bfe\u5fdc\u3057\u305f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u81ea\u52d5\u7684\u306b\u63a2\u305d\u3046\u3068\u3057\u307e\u3059\u3002\u5bfe\u5fdc\u3059\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7528\u610f\u3057\u306a\u3044\u5834\u5408\u306f\u3001<code>self.abstract_class = true<\/code> \u3092\u66f8\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002ActiveRecord::Base \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3057\u3001\u3055\u3089\u306b\u305d\u306e\u30af\u30e9\u30b9\u3092\u7d99\u627f\u3055\u305b\u305f\u3044\u5834\u5408\u306b <code>self.abstract_class = true<\/code> \u304c\u66f8\u304b\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306b Rails 5.0.1 \u306b\u3066\u691c\u8a3c\u3057\u307e\u3057\u305f\u3002\u4f8b\u3048\u3070\u3001app\/models \u4ee5\u4e0b\u306b <code>ActiveRecord::Base<\/code> \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9 Animal \u3092\u4f5c\u308a\u307e\u3059\u3002\u305d\u3057\u3066\u30af\u30e9\u30b9 Animal \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9 Dog \u3092\u4f5c\u308a\u307e\u3059\u3002\u305d\u3057\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u306f dogs \u3068\u3044\u3046\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<p><strong>app\/models\/animal.rb<\/strong><\/p>\n<pre><code class=\"ruby\">class Animal &lt; ActiveRecord::Base\nend\n<\/code><\/pre>\n<p><strong>app\/models\/dog.rb<\/strong><\/p>\n<pre><code class=\"ruby\">class Dog &lt; Animal\nend\n<\/code><\/pre>\n<p>\u3053\u306e\u72b6\u614b\u3067\u3001Rails Console \u3092\u8d77\u52d5\u3057\u3066 <code>dog = Dog.new<\/code> \u3092\u884c\u3046\u3068\u3001<code>ActiveRecord::StatementInvalid: Could not find table 'animals'<\/code> \u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002\u3053\u308c\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b animals \u3068\u3044\u3046\u30c6\u30fc\u30d6\u30eb\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u305f\u3081\u3067\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u30af\u30e9\u30b9 Animal \u306b <code>self.abstract_class = true<\/code> \u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/models\/animal.rb<\/strong><\/strong><\/p>\n<pre><code class=\"ruby\">class Animal &lt; ActiveRecord::Base\n  self.abstract_class = true\nend\n<\/code><\/pre>\n<p><strong>app\/models\/dog.rb<\/strong><\/p>\n<pre><code class=\"ruby\">class Dog &lt; Animal\nend\n<\/code><\/pre>\n<p>\u3053\u306e\u72b6\u614b\u3067\u3001Rails Console \u3092\u8d77\u52d5\u3057\u3066 <code>dog = Dog.new<\/code> \u3092\u884c\u3046\u3068\u30a8\u30e9\u30fc\u304c\u51fa\u307e\u305b\u3093\u3002<\/p>\n<p>\u53c2\u8003<br \/>\n<a href=\"http:\/\/api.rubyonrails.org\/classes\/ActiveRecord\/Inheritance\/ClassMethods.html\" target=\"_blank\">http:\/\/api.rubyonrails.org\/classes\/ActiveRecord\/Inheritance\/ClassMethods.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ActiveRecord::Base \u3092\u7d99\u627f\u3057\u305f\u30af\u30e9\u30b9\u3092\u30e2\u30c7\u30eb\u3068\u3057\u3066\u4f5c\u6210\u3059\u308b\u3068\u3001Rails \u306f\u305d\u306e\u30af\u30e9\u30b9\u540d\u306b\u5bfe\u5fdc\u3057\u305f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u81ea\u52d5\u7684\u306b\u63a2\u305d\u3046\u3068\u3057\u307e\u3059\u3002\u5bfe\u5fdc\u3059\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u7528\u610f\u3057\u306a\u3044\u5834\u5408\u306f\u3001sel [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[241],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8gd1W-zI","_links":{"self":[{"href":"https:\/\/codaholic.org\/index.php?rest_route=\/wp\/v2\/posts\/2214"}],"collection":[{"href":"https:\/\/codaholic.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codaholic.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codaholic.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codaholic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2214"}],"version-history":[{"count":52,"href":"https:\/\/codaholic.org\/index.php?rest_route=\/wp\/v2\/posts\/2214\/revisions"}],"predecessor-version":[{"id":2272,"href":"https:\/\/codaholic.org\/index.php?rest_route=\/wp\/v2\/posts\/2214\/revisions\/2272"}],"wp:attachment":[{"href":"https:\/\/codaholic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codaholic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codaholic.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}