{"id":1455,"date":"2022-01-01T08:29:08","date_gmt":"2021-12-31T23:29:08","guid":{"rendered":"https:\/\/suzutukiblog.com\/?p=1455"},"modified":"2022-03-07T20:42:04","modified_gmt":"2022-03-07T11:42:04","slug":"model","status":"publish","type":"post","link":"https:\/\/suzutukiblog.com\/index.php\/2022\/01\/01\/model\/","title":{"rendered":"1\u5bfe\u591a\u306e\u30e2\u30c7\u30eb\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h3>\u7d4c\u7def<\/h3>\n<p>\u958b\u767a\u3057\u3066\u3044\u308b\u30a2\u30d7\u30ea\u3067\u5fc5\u8981\u306b\u306a\u3063\u305f\u306e\u3067\u307e\u3068\u3081<\/p>\n<h3>\u4f55\u304c\u3057\u305f\u3044\u304b<\/h3>\n<p>users\u30c6\u30fc\u30d6\u30eb\u306b\u7d10\u4ed8\u3044\u3066\u3044\u308bIf\u306b\u306f\u8907\u6570\u306eThen\u304c\u3042\u308b\u3068\u3044\u3046\u306e\u3092\u5b9f\u88c5\u3057\u305f\u304b\u3063\u305f\u3002<\/p>\n<h3>\u53c2\u8003<\/h3>\n<p>Rails Tutorial14\u7ae0<\/p>\n<h3>\u30e2\u30c7\u30eb\u540c\u58eb\u306e\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7\u30f3\u306e\u8a18\u8ff0<\/h3>\n<h4>\u30e2\u30c7\u30eb\u3068\u30c6\u30fc\u30d6\u30eb\u306e\u95a2\u4fc21\uff08user\uff09<\/h4>\n<p>\u3082\u3068\u3068\u306a\u308buser\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<pre><span class=\"k\"> migration_file\r\n\r\nclass<\/span> <span class=\"nc\">CreateUsers<\/span> <span class=\"o\">&lt;<\/span> <span class=\"no\">ActiveRecord<\/span><span class=\"o\">::<\/span><span class=\"no\">Migration<\/span><span class=\"p\">[<\/span><span class=\"mf\">5.0<\/span><span class=\"p\">]<\/span>\r\n  <span class=\"k\">def<\/span> <span class=\"nf\">change<\/span>\r\n    <span class=\"n\">create_table<\/span> <span class=\"ss\">:users<\/span> <span class=\"k\">do<\/span> <span class=\"o\">|<\/span><span class=\"n\">t<\/span><span class=\"o\">|<\/span>\r\n      <span class=\"n\">t<\/span><span class=\"p\">.<\/span><span class=\"nf\">string<\/span>  <span class=\"ss\">:name<\/span><span class=\"p\">,<\/span> <span class=\"ss\">null: <\/span><span class=\"kp\">false<\/span>\r\n      <span class=\"n\">t<\/span><span class=\"p\">.<\/span><span class=\"nf\">string<\/span>  <span class=\"ss\">:email<\/span><span class=\"p\">,<\/span> <span class=\"ss\">null: <\/span><span class=\"kp\">false<\/span><span class=\"p\">,<\/span> <span class=\"ss\">unique: <\/span><span class=\"kp\">true<\/span> \r\n      <span class=\"n\">t<\/span><span class=\"p\">.<\/span><span class=\"nf\">timestamps<\/span>\r\n    <span class=\"k\">end<\/span>\r\n  <span class=\"k\">end<\/span>\r\n<span class=\"k\">end<\/span><\/pre>\n<p>\u30fbuser.rb<br \/>\nuser\u306f\u305f\u304f\u3055\u3093\u306esituation(if)\u3092\u6301\u3063\u3066\u3044\u3066\u3001<br \/>\nsituation(if)\u3092\u901a\u3057\u3066\u305f\u304f\u3055\u3093\u306ebehavior(then)\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u30e2\u30c7\u30eb\u3068\u30c6\u30fc\u30d6\u30eb\u306e\u95a2\u4fc22\uff08behavior\uff09<\/h3>\n<div class=\"code-lang\"><span class=\"bold\">migration_file<\/span><\/div>\n<div class=\"code-copy\"><\/div>\n<div class=\"highlight\">\n<pre>class CreateBehaviors &lt; ActiveRecord::Migration[5.1]\r\n  def change\r\n    create_table :behaviors do |t|\r\n      t.text :name, null: false, unique:true\r\n\r\n      t.timestamps\r\n    end\r\n  end\r\nend<\/pre>\n<\/div>\n<p>reference\uff08\u53c2\u7167\uff09\u306f\u30c6\u30fc\u30d6\u30eb\u540c\u58eb\u306e\u95a2\u4fc2\u6027\u3092\u793a\u3057\u307e\u3059\u3002<\/p>\n<h4>\u30e2\u30c7\u30eb\u3068\u30c6\u30fc\u30d6\u30eb\u306e\u95a2\u4fc23\uff08situation\uff09<\/h4>\n<p>\u3053\u3053\u3067behavior(then)\u3068user\u306e\u9593\u3092\u7d50\u3076\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u304c\u3001<br \/>\n\u305d\u3053\u3067<strong>reference<\/strong>\u3068\u3044\u3046\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<div class=\"highlight\">\n<pre>class CreateSituations &lt; ActiveRecord::Migration[5.1]\r\n  def change\r\n    create_table :situations do |t|\r\n      t.references :user, foreign_key: true\r\n      t.references :behavior, foreign_key: true\r\n\r\n      t.timestamps\r\n    end\r\n  end\r\nend<\/pre>\n<p>\u30fbuser.rb<br \/>\nuser\u306f\u305f\u304f\u3055\u3093\u306ebehavior(then)\u3092\u6301\u3063\u3066\u3044\u3066\u3001<br \/>\nsituation(if)\u3092\u901a\u3057\u3066\u305f\u304f\u3055\u3093\u306ebehavior(then)\u3092\u6301\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3092\u901a\u3057\u3066\u7e4b\u304c\u3063\u3066\u3044\u308b\u3082\u306e\u306b\u306f<br \/>\n<code>through: :situations<\/code>\u3068\u3044\u3046key\u3092\u3064\u3051\u307e\u3059\u3002<\/p>\n<div class=\"code-frame\" data-lang=\"ruby\">\n<div class=\"code-lang\"><span class=\"bold\">user.rb<\/span><\/div>\n<\/div>\n<div class=\"highlight\">\n<pre>class User &lt; ApplicationRecord\r\n  has_many :behaviors, through: :situations, dependent: :destroy\r\n  has_many :situations,<\/pre>\n<\/div>\n<h3>behavior.rb<\/h3>\n<p>User.rb\u3068\u307b\u307c\u540c\u3058\u3067\u3059\u3002<br \/>\n<code>accepts_nested_attributes_for<\/code> \u3068\u3044\u3046key\u306f\u3001<br \/>\n<strong>\u4ed6\u306e\u30e2\u30c7\u30eb\u3092\u4e00\u62ec\u3067\u66f4\u65b0\u3001\u4fdd\u5b58\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b<\/strong>\u3082\u306e\u3067\u3059\u3002<br \/>\n\u3053\u3053\u3067\u306fbehavior(then)\u3092\u4fdd\u5b58\u3059\u308b\u306e\u3068\u540c\u6642\u306bsituations\u3092\u66f4\u65b0\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"code-frame\" data-lang=\"ruby\">\n<h3 class=\"code-lang\"><span class=\"bold\">behavior.rb<\/span><\/h3>\n<\/div>\n<div class=\"highlight\">\n<pre>class Behavior &lt; ApplicationRecord\r\n  has_many :users, through: :situations\r\n  has_many :situations\r\n  accepts_nested_attributes_for :situations\r\nend<\/pre>\n<\/div>\n<\/div>\n<h3>situation.rb<\/h3>\n<p>group_user\u306f1\u3064\u306euser\u30011\u3064\u306ebehavior\u306b\u5c5e\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3088\u3063\u3066\u3001\u3053\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre>class Situation &lt; ApplicationRecord\r\n  belongs_to :user\r\n  belongs_to :behavior\r\nend<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7d4c\u7def \u958b\u767a\u3057\u3066\u3044\u308b\u30a2\u30d7\u30ea\u3067\u5fc5\u8981\u306b\u306a\u3063\u305f\u306e\u3067\u307e\u3068\u3081 \u4f55\u304c\u3057\u305f\u3044\u304b users\u30c6\u30fc\u30d6\u30eb\u306b\u7d10\u4ed8\u3044\u3066\u3044\u308bIf\u306b\u306f\u8907\u6570\u306eThen\u304c\u3042\u308b\u3068\u3044\u3046\u306e\u3092\u5b9f\u88c5\u3057\u305f\u304b\u3063\u305f\u3002 \u53c2\u8003 Rails Tutorial14\u7ae0 \u30e2\u30c7\u30eb\u540c\u58eb\u306e\u30a2\u30bd\u30b7\u30a8\u30fc\u30b7\u30e7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,13],"tags":[],"class_list":["post-1455","post","type-post","status-publish","format-standard","hentry","category-programming-note","category-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/1455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=1455"}],"version-history":[{"count":8,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/1455\/revisions"}],"predecessor-version":[{"id":5076,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/1455\/revisions\/5076"}],"wp:attachment":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=1455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=1455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=1455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}