teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

モデルのコードを追加

2018/11/21 09:54

投稿

ayachika
ayachika

スコア36

title CHANGED
File without changes
body CHANGED
@@ -170,4 +170,88 @@
170
170
  railsのバージョンはRails 5.0.7 です。
171
171
 
172
172
  どなたか解決方法をご存知の方がいらっしゃればご教示いただけないでしょうか。
173
- よろしくお願いします。
173
+ よろしくお願いします。
174
+
175
+
176
+ 【追記】
177
+ モデルのコードを掲載します。
178
+ user.rb
179
+
180
+ ```
181
+ class User < ApplicationRecord
182
+ attr_accessor :remember_token
183
+ has_many :favorites, through: :favorites, source: :post, dependent: :destroy
184
+ has_many :posts, through: :favorites, dependent: :destroy ,foreign_key: 'user'
185
+
186
+ before_save { email.downcase! }
187
+ validates :name, presence: true, length: { maximum: 50 }
188
+ VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i
189
+ validates :email, presence: true, length: { maximum: 255 },
190
+ format: { with: VALID_EMAIL_REGEX },
191
+ uniqueness: { case_sensitive: false }
192
+ has_secure_password
193
+ validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
194
+
195
+
196
+
197
+ def self.digest(string)
198
+ cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
199
+ BCrypt::Engine.cost
200
+ BCrypt::Password.create(string, cost: cost)
201
+ end
202
+
203
+
204
+ def self.new_token
205
+ SecureRandom.urlsafe_base64
206
+ end
207
+
208
+
209
+ def remember
210
+ self.remember_token = User.new_token
211
+ update_attribute(:remember_digest, User.digest(remember_token))
212
+ end
213
+
214
+ def authenticated?(remember_token)
215
+ return false if remember_digest.nil?
216
+ BCrypt::Password.new(remember_digest).is_password?(remember_token)
217
+ end
218
+
219
+
220
+ def forget
221
+ update_attribute(:remember_digest, nil)
222
+ end
223
+
224
+
225
+ end
226
+ ```
227
+
228
+ post.rb
229
+
230
+ ```
231
+ class Post < ApplicationRecord
232
+
233
+ has_many :posts, dependent: :destroy
234
+ has_many :comments, dependent: :delete_all
235
+ has_many :post_tag_relations, dependent: :delete_all
236
+ has_many :tags, through: :post_tag_relations
237
+ belongs_to :user
238
+ validates :user_id, presence: true
239
+ default_scope -> { order(created_at: :desc) }
240
+ # presence ->
241
+ # length ->
242
+ # validates :name, presence: true, length: { maximum: 30 }
243
+ validates :title, presence: true, length: { maximum: 30 }
244
+ validates :content, presence: true, length: { maximum: 1000 }
245
+
246
+
247
+ mount_uploader :picture, PictureUploader
248
+ validates :user, presence:true
249
+
250
+ has_many :favorites, dependent: :destroy
251
+ has_many :users, through: :favorites
252
+ validates :user_id,presence: true
253
+ validates :content, presence: true, length: { maximum: 140 }
254
+
255
+ end
256
+
257
+ ```

1

開発環境を追加

2018/11/21 09:54

投稿

ayachika
ayachika

スコア36

title CHANGED
File without changes
body CHANGED
@@ -164,5 +164,10 @@
164
164
  また、以下のサイトも参考にし、確認してみましたが当方と異なるパターンだったため、こちらに質問させていただきました。
165
165
  [stack level too deepのエラーを解決したい](https://teratail.com/questions/56653)
166
166
 
167
+ ##開発環境
168
+ AWSのcloud9を使用しています。
169
+ rubyのバージョンは 2.4.1p111
170
+ railsのバージョンはRails 5.0.7 です。
171
+
167
172
  どなたか解決方法をご存知の方がいらっしゃればご教示いただけないでしょうか。
168
173
  よろしくお願いします。