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

質問編集履歴

6

内容の更新

2020/04/16 05:46

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,24 +1,32 @@
1
1
  独自メソッドを用いてvalidateを行いたいと考えていましたがうまく行きません。ネット上にはhas_manyのバリデーションの仕方など掲載されておりましたがthroughを含めたものは見た限りありませんでした。
2
- 自分が開発しているアプリ内でフォローできる人数を1人以下にしたいです。つまり、フォローできる人数を0人か1人に限りたいです。そこでバリデーションを設置する事で2人以上の人をフォローをした時にエラーが表示されるようにしたいです。
2
+ 自分が開発しているアプリ内でフォローできる人数を8人以下にしたいです。そこでバリデーションを設置する事で8人以上の人をフォローをした時にエラーが表示されるようにしたいです。
3
3
 
4
- 以下のコードではバリデーションに引っかからず2人以上の人をフォローできるようになってしまいます。
4
+ 以下のコードではバリデーションに引っかからず8人以上の人をフォローできるようになってしまいます。
5
5
  よろしくお願いします。
6
6
 
7
7
  やった事として、rails consoleを開き
8
8
  ```ここに言語を入力
9
9
  first_following=Relationship.create(follower_id: "1", followed_id: "2")
10
10
  second_following=Relationship.create(follower_id: "1", followed_id: "3")
11
+ third_following=Relationship.create(follower_id: "1", followed_id: "4")
12
+ fourth_following=Relationship.create(follower_id: "1", followed_id: "5")
13
+ fifth_following=Relationship.create(follower_id: "1", followed_id: "6")
14
+ sixth_following=Relationship.create(follower_id: "1", followed_id: "7")
15
+ seventh_following=Relationship.create(follower_id: "1", followed_id: "8")
16
+ eighth_following=Relationship.create(follower_id: "1", followed_id: "9")
17
+ ninth_following=Relationship.create(follower_id: "1", followed_id: "10")
18
+
11
19
  ```
12
- 上のコードを入力してレコードを作成し、つともレコードが作成されました。エラーやレコードを作成できない
20
+ 上のコードを入力してレコードを作成し、つともレコードが作成されました。エラーやレコードを作成できない
13
21
  といったメッセージは表示されませんでした。
14
- 確認として,以下のコードを入力してが出力され、人フォローできていることも確認できました。
22
+ 確認として,以下のコードを入力してが出力され、人フォローできていることも確認できました。
15
23
  ```ここに言語を入力
16
24
  me=Account.find_by(id:"1")
17
25
  me.following.count
18
26
  ```
19
27
 
20
28
 
21
- これでバリデーションに問題があるというのはわかったのですが実際にどこに問題があり、なぜバリデーションが機能せず、人以上フォローすることができてしまうのかがよくわかりません。
29
+ これでバリデーションに問題があるというのはわかったのですが実際にどこに問題があり、なぜバリデーションが機能せず、人以上フォローすることができてしまうのかがよくわかりません。
22
30
 
23
31
 
24
32
 
@@ -87,7 +95,7 @@
87
95
 
88
96
  def check_following
89
97
 
90
- if Account.find(followed_id).following.count > 1
98
+ if Account.find(followed_id).following.count > 8
91
99
 
92
100
  errors.add(:followed_id, "too much following")
93
101
 

5

問題の明確化

2020/04/16 05:46

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -18,6 +18,11 @@
18
18
  ```
19
19
 
20
20
 
21
+ これでバリデーションに問題があるというのはわかったのですが実際にどこに問題があり、なぜバリデーションが機能せず、2人以上フォローすることができてしまうのかがよくわかりません。
22
+
23
+
24
+
25
+
21
26
  account.rb
22
27
  ```ここに言語を入力
23
28
  class Account < ApplicationRecord

4

質問のアップデート

2020/04/16 05:36

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -1,9 +1,23 @@
1
1
  独自メソッドを用いてvalidateを行いたいと考えていましたがうまく行きません。ネット上にはhas_manyのバリデーションの仕方など掲載されておりましたがthroughを含めたものは見た限りありませんでした。
2
- フォローできる人数を人以下にしたいです。
2
+ 自分が開発しているアプリ内でフォローできる人数を1人以下にしたいです。つまり、フォローできる人数を0人か1人に限りたいです。そこでバリデーションを設置する事で2人以上の人をフォローをした時にエラーが表示されるようにしたいです。
3
+
3
- 以下のコードではバリデーションに引っかからず人以上人をフォローできるようになってしまいます。
4
+ 以下のコードではバリデーションに引っかからず2人以上人をフォローできるようになってしまいます。
4
5
  よろしくお願いします。
5
6
 
7
+ やった事として、rails consoleを開き
8
+ ```ここに言語を入力
9
+ first_following=Relationship.create(follower_id: "1", followed_id: "2")
10
+ second_following=Relationship.create(follower_id: "1", followed_id: "3")
11
+ ```
12
+ 上のコードを入力してレコードを作成し、2つともレコードが作成されました。エラーやレコードを作成できない
13
+ といったメッセージは表示されませんでした。
14
+ 確認として,以下のコードを入力して2が出力され、2人フォローできていることも確認できました。
15
+ ```ここに言語を入力
16
+ me=Account.find_by(id:"1")
17
+ me.following.count
18
+ ```
6
19
 
20
+
7
21
  account.rb
8
22
  ```ここに言語を入力
9
23
  class Account < ApplicationRecord
@@ -68,7 +82,7 @@
68
82
 
69
83
  def check_following
70
84
 
71
- if Account.find(followed_id).following.count >= 1
85
+ if Account.find(followed_id).following.count > 1
72
86
 
73
87
  errors.add(:followed_id, "too much following")
74
88
 

3

コード修正

2020/04/16 05:34

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -68,9 +68,9 @@
68
68
 
69
69
  def check_following
70
70
 
71
- if active_relationships.following.count > 3
71
+ if Account.find(followed_id).following.count >= 1
72
72
 
73
- errors.add(:following, "too much following")
73
+ errors.add(:followed_id, "too much following")
74
74
 
75
75
 
76
76
  end

2

コード編集

2020/02/29 09:19

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,7 @@
10
10
  # Include default devise modules. Others available are:
11
11
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
12
12
 
13
- validate :check_following
13
+
14
14
  devise :database_authenticatable, :registerable,
15
15
  :recoverable, :rememberable, :validatable
16
16
 
@@ -46,15 +46,7 @@
46
46
 
47
47
 
48
48
 
49
- def check_following
49
+
50
-
51
- if active_relationships.following.count > 3
52
-
53
- errors.add(:following, "too much following")
54
-
55
-
56
- end
57
- end
58
50
 
59
51
 
60
52
 
@@ -72,8 +64,18 @@
72
64
  validates :followed_id, presence: true
73
65
  belongs_to :follower, class_name: "Account", optional: true
74
66
  belongs_to :followed, class_name: "Account", optional: true
67
+ validate :check_following
68
+
69
+ def check_following
70
+
71
+ if active_relationships.following.count > 3
72
+
73
+ errors.add(:following, "too much following")
74
+
75
+
76
+ end
77
+ end
75
78
 
76
-
77
79
  end
78
80
 
79
81
  ```

1

タイトル変更

2020/02/29 01:48

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- validate has_many through が機能しません
1
+ validate(バリデーション)が機能しません
body CHANGED
File without changes