質問編集履歴
1
項目を追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -70,4 +70,70 @@
|
|
70
70
|
```
|
71
71
|
|
72
72
|
|
73
|
-
申し訳ございませんが、対応方法のご教示をお願い致します。
|
73
|
+
申し訳ございませんが、対応方法のご教示をお願い致します。
|
74
|
+
|
75
|
+
追記です。
|
76
|
+
外部キーの設定を誤ってしまった可能性があります・・・
|
77
|
+
もし訂正する必要がございましたら、方法をご教示いただけますでしょうか?
|
78
|
+
```ここに言語を入力
|
79
|
+
class CreatePost01s < ActiveRecord::Migration[5.1]
|
80
|
+
def change
|
81
|
+
create_table :post01s do |t|
|
82
|
+
t.references :user, foreign_key: true
|
83
|
+
t.text :caption
|
84
|
+
|
85
|
+
t.timestamps
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class CreatePostImage01s < ActiveRecord::Migration[5.1]
|
91
|
+
def change
|
92
|
+
create_table :post_image01s do |t|
|
93
|
+
t.references :post, foreign_key: true
|
94
|
+
t.string :name
|
95
|
+
|
96
|
+
t.timestamps
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
モデル
|
102
|
+
```ここに言語を入力
|
103
|
+
class Post01< ApplicationRecord
|
104
|
+
belongs_to :user01
|
105
|
+
has_many :post_image01s, dependent: :destroy
|
106
|
+
end
|
107
|
+
|
108
|
+
class PostImage01 < ApplicationRecord
|
109
|
+
belongs_to :post01
|
110
|
+
end
|
111
|
+
|
112
|
+
class User01 < ApplicationRecord
|
113
|
+
#リレーション(1:N)
|
114
|
+
has_many :post01s
|
115
|
+
#データの保存前に、パスワードを暗号化するメゾット(convert_passworc)を実行するよう設定
|
116
|
+
before_save :convert_password
|
117
|
+
|
118
|
+
#パスワードを暗号化するメゾット
|
119
|
+
def convert_password
|
120
|
+
self.password = User01.generate_password(self.password)
|
121
|
+
end
|
122
|
+
|
123
|
+
#パスワードをmd5に変換するメゾット
|
124
|
+
def self.generate_password(password)
|
125
|
+
#パスワードに適当な文字列を付加して暗号化する
|
126
|
+
salt = "h!hgamcRAdh38bajhvgai17ysvb"
|
127
|
+
Digest::MD5.hexdigest(salt + password)
|
128
|
+
end
|
129
|
+
|
130
|
+
#バリテーション
|
131
|
+
VALID_EMAIL_REGEX = /\A[\w\-.]+@[a-z\d\-.]+.[a-z]+\z/i
|
132
|
+
validates :name, presence: true
|
133
|
+
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: true
|
134
|
+
validates :password, presence: true, length:{minimum: 6}
|
135
|
+
end
|
136
|
+
|
137
|
+
宜しくお願い致します。
|
138
|
+
|
139
|
+
```
|