{"id":610,"date":"2018-06-03T21:21:18","date_gmt":"2018-06-03T12:21:18","guid":{"rendered":"https:\/\/yuis-programming.com\/?p=610"},"modified":"2018-05-01T01:21:26","modified_gmt":"2018-04-30T16:21:26","slug":"rails%e3%81%ae%e3%83%a2%e3%83%87%e3%83%ab%e3%81%ae%e4%b8%80%e5%af%be%e5%a4%9a%e3%82%92%e3%82%84%e3%81%a3%e3%81%a6%e3%81%bf%e3%82%88%e3%81%86","status":"publish","type":"post","link":"https:\/\/yuis-programming.com\/?p=610","title":{"rendered":"Rails\u306e\u30e2\u30c7\u30eb\u306e\u4e00\u5bfe\u591a\u3092\u3084\u3063\u3066\u307f\u3088\u3046"},"content":{"rendered":"<p>\u6b21\u306e\u3088\u3046\u306b\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001migrate\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre><code>rails g model user name:string description:text icon:binary\n\nrails g model hashmodel title:string content:text vote:integer favorite:integer views:integer user_id:integer\n#=&gt; \u30e2\u30c7\u30eb\u540d\"hash\"\u3060\u3068\u65e2\u306bRuby\u306e\u30af\u30e9\u30b9\u3068\u3057\u3066hash\u304c\u3042\u308b\u306e\u3067\u3001\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002\n\nrails g model hashmodel title:string content:text vote:integer favorite:integer views:integer user_id:references #=&gt; \u591a\u5206\u9593\u9055\u3044\u3002\nrails g model hashmodel title:string content:text vote:integer favorite:integer views:integer user:references\n#=&gt; `user_id:references`({\u4e00\u5bfe\u591a\u306e\u4e00\u30e2\u30c7\u30eb\u540d}_id)\u3068\u3059\u308b\u3053\u3068\u3067\u3001\u5f8c\u8ff0\u306e`belongs_to :user`\u3092\u8ffd\u8a18\u3059\u308b\u624b\u9593\u304c\u7701\u3051\u308b\u3002\u305d\u308c\u3060\u3051\u3002\n<\/code><\/pre>\n<p>\u6b21\u306e\u30b3\u30de\u30f3\u30c9\u3067\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p><code>rake db:migrate<\/code><\/p>\n<p><code>belongs_to<\/code>\u3067&#8221;\u4e00&#8221;\u30e2\u30c7\u30eb\u3078\u95a2\u9023\u4ed8\u3051\u3057\u307e\u3059\u3002<\/p>\n<p>models\/hashmodel.rb<\/p>\n<pre><code>class Hashmodel &lt; ApplicationRecord\n  belongs_to :user\nend\n<\/code><\/pre>\n<p><code>has_many<\/code>\u3067&#8221;\u591a&#8221;\u30e2\u30c7\u30eb\u3078\u95a2\u9023\u4ed8\u3051\u3057\u307e\u3059\u3002<\/p>\n<p>models\/user.rb<\/p>\n<pre><code>class User &lt; ApplicationRecord\n  has_many :hashmodels, dependent: :destroy\nend\n<\/code><\/pre>\n<p><code>dependent: :destroy<\/code>\u304c\u3042\u308b\u3053\u3068\u3067\u3001\u30e6\u30fc\u30b6\u30fcA\u3092\u524a\u9664\u3059\u308b\u3068\u3001\u81ea\u52d5\u7684\u306b\u30e6\u30fc\u30b6\u30fcA\u304c\u4f5c\u6210\u3057\u305fhashmodel\u3082\u5168\u3066\u524a\u9664\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u307e\u3041\u3001\u4e00\u5bfe\u591a\u306e\u30c6\u30f3\u30d7\u30ec\u307f\u305f\u3044\u306a\u3082\u306e\u3067\u3059\u306d\u3002<\/p>\n<p>\u3053\u308c\u3067\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\u53ca\u3073\u3001\u4e00\u5bfe\u591a\u306e\u95a2\u9023\u4ed8\u3051\u306f\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n<h2>\u4e00\u5bfe\u591a\u306e\u95a2\u9023\u4ed8\u3051\u3092\u3057\u305f\u72b6\u614b\u3067\u8272\u3005\u3084\u3063\u3066\u307f\u308b\u3002<\/h2>\n<p>\u307e\u305a\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\u3002<\/p>\n<pre><code>@user = User.create(name: \"yuis\", description: \"hello. I'm yuis\")\n<\/code><\/pre>\n<p>\u305d\u306e\u30e6\u30fc\u30b6\u30fc\u3067\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u4f5c\u6210\u3002<\/p>\n<pre><code>@hashmodel = @user.hashmodels.create(title: \"title.\" ,content: \"content.\")\n<\/code><\/pre>\n<p>\u3067\u304d\u305f\u3002<\/p>\n<p>\u65b0\u898f\u4f5c\u6210\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u304c\u65b0\u898f\u4f5c\u6210\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u4f5c\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3002<\/p>\n<pre><code>@hashmodel = User.create(name: \"nanashi\").hashmodels.create(title: \"title.\" ,content: \"content.\")\n<\/code><\/pre>\n<h3>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4e2d\u8eab\u3092\u78ba\u8a8d\u3057\u3066\u307f\u3088\u3046\u3002<\/h3>\n<p>\u3067\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u4e2d\u8eab\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u3061\u3083\u3093\u3068<code>hashmodel<\/code>\u306e<code>user_id<\/code>\u304c\u4e8c\u4eba\u5206\u306b\u306a\u3063\u3066\u3044\u308b\u304b\u304c\u30ad\u30e2\u3067\u3059\u306d\u3002(\u4e00\u5bfe\u591a\u306e\u95a2\u4fc2)<\/p>\n<p>\u307e\u305a\u3001users\u30c6\u30fc\u30d6\u30eb\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>User.all\n  User Load (0.0ms)  SELECT \"users\".* FROM \"users\"\n+----+---------+----------------+------+-------------------------+-------------------------+\n| id | name    | description    | icon | created_at              | updated_at              |\n+----+---------+----------------+------+-------------------------+-------------------------+\n| 1  | yuisice | \u3088\u308d\u3057\u304f\u3067\u3059\u3002 |      | 2018-04-28 10:09:30 UTC | 2018-04-28 10:11:40 UTC |\n| 2  | nanashi |                |      | 2018-04-28 10:14:47 UTC | 2018-04-28 10:14:47 UTC |\n+----+---------+----------------+------+-------------------------+-------------------------+\n2 rows in set\n<\/code><\/pre>\n<p>\u3061\u3083\u3093\u3068\u51fa\u6765\u3066\u307e\u3059\u306d\u3002<\/p>\n<p>\u3067\u306f\u3001hashmodels\u30c6\u30fc\u30d6\u30eb\u3002<\/p>\n<pre><code>Hashmodel.all\n  Hashmodel Load (0.5ms)  SELECT \"hashmodels\".* FROM \"hashmodels\"\n+----+----------+------------+------+----------+-------+---------+-------------------------+-------------------------+\n| id | title    | content    | vote | favorite | views | user_id | created_at              | updated_at              |\n+----+----------+------------+------+----------+-------+---------+-------------------------+-------------------------+\n| 1  | title.   | content.   |      |          |       | 1       | 2018-04-28 10:12:29 UTC | 2018-04-28 10:12:29 UTC |\n| 2  | title._2 | content._2 |      |          |       | 1       | 2018-04-28 10:13:31 UTC | 2018-04-28 10:13:31 UTC |\n| 3  | title.   | content.   |      |          |       | 2       | 2018-04-28 10:14:47 UTC | 2018-04-28 10:14:47 UTC |\n+----+----------+------------+------+----------+-------+---------+-------------------------+-------------------------+\n3 rows in set\n<\/code><\/pre>\n<p>\u51fa\u6765\u3066\u307e\u3059\u306d\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6b21\u306e\u3088\u3046\u306b\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u3001migrate\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002 rails g model user na [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"acf":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8Rxwo-9Q","_links":{"self":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/610"}],"collection":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=610"}],"version-history":[{"count":1,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions"}],"predecessor-version":[{"id":611,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions\/611"}],"wp:attachment":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}