質問編集履歴

1

アソシエーションのコードの記述

2017/03/21 14:24

投稿

lapi
lapi

スコア58

test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,89 @@
45
45
  どのように対処したらいいかご教授頂けると助かります。
46
46
 
47
47
  よろしくお願いします。
48
+
49
+
50
+
51
+ ###コード
52
+
53
+ アソシエーションはこのように定義しています
54
+
55
+
56
+
57
+ ``ブログ機能的なthreモデル[thre.rb]``
58
+
59
+
60
+
61
+ ```thre.rb
62
+
63
+ class Thre < ActiveRecord::Base
64
+
65
+ belongs_to :user
66
+
67
+ # CommentthモデルのAssociationを設定
68
+
69
+ has_many :commentths, dependent: :destroy
70
+
71
+ end
72
+
73
+ ```
74
+
75
+
76
+
77
+ ``userモデル``
78
+
79
+
80
+
81
+ ```user.rb
82
+
83
+ class User < ActiveRecord::Base
84
+
85
+ # Include default devise modules. Others available are:
86
+
87
+ # :confirmable, :lockable, :timeoutable and :omniauthable
88
+
89
+
90
+
91
+ 一部省略
92
+
93
+
94
+
95
+ # UserモデルのレコードがThreモデルのレコードを複数もつことを定義
96
+
97
+ has_many :thres, dependent: :destroy
98
+
99
+
100
+
101
+ # CommentthモデルのAssociationを設定
102
+
103
+ has_many :commentths, dependent: :destroy
104
+
105
+
106
+
107
+ 省略
108
+
109
+ end
110
+
111
+ ```
112
+
113
+
114
+
115
+ ``commentthモデル``
116
+
117
+
118
+
119
+ ```
120
+
121
+ class Commentth < ActiveRecord::Base
122
+
123
+ belongs_to :user
124
+
125
+ belongs_to :thre
126
+
127
+ end
128
+
129
+ ```
130
+
131
+
132
+
133
+ 現在アソシエーションはこのように記述しています。