質問編集履歴
6
内容の更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,24 +1,32 @@
|
|
1
1
|
独自メソッドを用いてvalidateを行いたいと考えていましたがうまく行きません。ネット上にはhas_manyのバリデーションの仕方など掲載されておりましたがthroughを含めたものは見た限りありませんでした。
|
2
|
-
自分が開発しているアプリ内でフォローできる人数を
|
2
|
+
自分が開発しているアプリ内でフォローできる人数を8人以下にしたいです。そこでバリデーションを設置する事で8人以上の人をフォローをした時にエラーが表示されるようにしたいです。
|
3
3
|
|
4
|
-
以下のコードではバリデーションに引っかからず
|
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
|
+
上のコードを入力してレコードを作成し、9つともレコードが作成されました。エラーやレコードを作成できない
|
13
21
|
といったメッセージは表示されませんでした。
|
14
|
-
確認として,以下のコードを入力して
|
22
|
+
確認として,以下のコードを入力して9が出力され、9人フォローできていることも確認できました。
|
15
23
|
```ここに言語を入力
|
16
24
|
me=Account.find_by(id:"1")
|
17
25
|
me.following.count
|
18
26
|
```
|
19
27
|
|
20
28
|
|
21
|
-
これでバリデーションに問題があるというのはわかったのですが実際にどこに問題があり、なぜバリデーションが機能せず、
|
29
|
+
これでバリデーションに問題があるというのはわかったのですが実際にどこに問題があり、なぜバリデーションが機能せず、9人以上フォローすることができてしまうのかがよくわかりません。
|
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 >
|
98
|
+
if Account.find(followed_id).following.count > 8
|
91
99
|
|
92
100
|
errors.add(:followed_id, "too much following")
|
93
101
|
|
5
問題の明確化
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
質問のアップデート
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 >
|
85
|
+
if Account.find(followed_id).following.count > 1
|
72
86
|
|
73
87
|
errors.add(:followed_id, "too much following")
|
74
88
|
|
3
コード修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -68,9 +68,9 @@
|
|
68
68
|
|
69
69
|
def check_following
|
70
70
|
|
71
|
-
if
|
71
|
+
if Account.find(followed_id).following.count >= 1
|
72
72
|
|
73
|
-
errors.add(:
|
73
|
+
errors.add(:followed_id, "too much following")
|
74
74
|
|
75
75
|
|
76
76
|
end
|
2
コード編集
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
|
-
|
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
|
-
|
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
タイトル変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
validate
|
1
|
+
validate(バリデーション)が機能しません
|
body
CHANGED
File without changes
|