質問編集履歴

1

追記依頼の、アソシエーション部分を追記

2019/12/02 00:27

投稿

garta
garta

スコア15

test CHANGED
File without changes
test CHANGED
@@ -95,3 +95,117 @@
95
95
  <% end %>
96
96
 
97
97
  ```
98
+
99
+ ###like.rb
100
+
101
+ ```ここに言語を入力
102
+
103
+ class Like < ApplicationRecord
104
+
105
+ belongs_to :user
106
+
107
+ belongs_to :micropost
108
+
109
+ validates :user_id, presence: true
110
+
111
+ validates :like_id, presence: true
112
+
113
+ end
114
+
115
+ ```
116
+
117
+ ###user.rb
118
+
119
+ ```ここに言語を入力
120
+
121
+ class User < ApplicationRecord
122
+
123
+ has_many :microposts,dependent: :destroy
124
+
125
+ has_many :active_relationships, class_name: "Relationship",
126
+
127
+ foreign_key: "follower_id",
128
+
129
+ dependent: :destroy
130
+
131
+ has_many :passive_relationships, class_name: "Relationship",
132
+
133
+ foreign_key: "followed_id",
134
+
135
+ dependent: :destroy
136
+
137
+ has_many :following, through: :active_relationships, source: :followed
138
+
139
+ has_many :followers, through: :passive_relationships
140
+
141
+ has_many :likes, dependent: :destroy
142
+
143
+
144
+
145
+ validates :name,presence: :true, length:{ maximum: 15 }
146
+
147
+ validates :email,presence: :true
148
+
149
+ validates :password, presence:true,length:{ minimum: 6 }
150
+
151
+ validates :password_confirmation,presence: :true
152
+
153
+
154
+
155
+ devise :database_authenticatable, :registerable,
156
+
157
+ :recoverable, :rememberable, :validatable
158
+
159
+
160
+
161
+ mount_uploader :avatar, AvatarUploader
162
+
163
+ ```
164
+
165
+ ###micropost.rb
166
+
167
+ ```ここに言語を入力
168
+
169
+ belongs_to :user
170
+
171
+ has_many :likes, dependent: :destroy
172
+
173
+ has_many :like_users, through: :likes, source: :user
174
+
175
+ default_scope -> { order(created_at: :desc) }
176
+
177
+ mount_uploader :picture, PictureUploader
178
+
179
+ validates :user_id,presence: true
180
+
181
+ validates :title, presence: true
182
+
183
+ validates :content,length: { maximum: 140 }
184
+
185
+ validates :picture, presence: true
186
+
187
+
188
+
189
+ ```
190
+
191
+ ###relationship.rb
192
+
193
+ ```ここに言語を入力
194
+
195
+ class Relationship < ApplicationRecord
196
+
197
+ belongs_to :follower, class_name: "User"
198
+
199
+ belongs_to :followed, class_name: "User"
200
+
201
+
202
+
203
+ validates :follower_id,presence: true
204
+
205
+ validates :followed_id,presence: true
206
+
207
+ end
208
+
209
+
210
+
211
+ ```