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

質問編集履歴

1

モデル追加

2019/12/05 14:09

投稿

tenten11055
tenten11055

スコア67

title CHANGED
File without changes
body CHANGED
@@ -115,4 +115,181 @@
115
115
  なぜ保存されてしまうのか、教えていただけると助かります。
116
116
  宜しくお願い致します。
117
117
 
118
- ※ストロングパラメーターの`su_account_id`はあってもなくても同じ結果でした。
118
+ ※ストロングパラメーターの`su_account_id`はあってもなくても同じ結果でした。
119
+
120
+
121
+ モデルを追記しました。
122
+ リレーションが怪しい気はしています。。
123
+
124
+ ```model
125
+ class SuAccount < ApplicationRecord
126
+ # 論理削除使用
127
+ acts_as_paranoid
128
+
129
+ ##
130
+ # relations
131
+ ##
132
+
133
+ has_one :su_company, class_name: 'SuCompanyInfo'
134
+ has_one :user, class_name: 'User'
135
+
136
+ accepts_nested_attributes_for :su_company, allow_destroy: true
137
+ accepts_nested_attributes_for :user, allow_destroy: true
138
+
139
+ ##
140
+ # validates
141
+ ##
142
+
143
+ # 部署名
144
+ validates :group_name,
145
+ presence: true,
146
+ length: { maximum: 100 }
147
+
148
+ # URL形式チェック
149
+ if Rails.env == 'production'
150
+ validates :image_url, format: /\A#{URI::regexp(%w(http https))}\z/
151
+ end
152
+ end
153
+
154
+ ```
155
+ ```
156
+ class SuCompanyInfo < ApplicationRecord
157
+ # 論理削除用
158
+ acts_as_paranoid
159
+
160
+ ##
161
+ # relations
162
+ ##
163
+
164
+ belongs_to :su_account, optional: true
165
+
166
+ ##
167
+ # validates
168
+ ##
169
+
170
+ # 企業名
171
+ validates :company_name,
172
+ presence: true,
173
+ length: { maximum: 100 }
174
+
175
+ # 企業HP
176
+ validates :company_hp,
177
+ length: { maximum: 2048 },
178
+ format: /\A#{URI::regexp(%w(http https))}\z/
179
+
180
+ # 設立年月日
181
+ validates :establishment_on,
182
+ date: true,
183
+ reduce: true
184
+
185
+ validates :establishment_year,
186
+ presence: true, if: -> { establishment_month.presence || establishment_date.presence }
187
+
188
+ validates :establishment_month,
189
+ presence: true, if: -> { establishment_year.presence }
190
+
191
+ validates :establishment_date,
192
+ presence: true, if: -> { establishment_year.presence }
193
+
194
+ # 代表者
195
+ validates :representative,
196
+ length: { maximum: 50 }
197
+
198
+ # 事業内容
199
+ validates :business_description,
200
+ length: { maximum: 500 }
201
+
202
+ # 資本金
203
+ validates :capital,
204
+ length: { maximum: 20 },
205
+ numericality: {
206
+ only_integer: true,
207
+ allow_blank: true
208
+ }
209
+
210
+ # 通貨単位
211
+ validates :capital_unit,
212
+ presence: true, if: -> { capital.presence }
213
+
214
+ # 所在地
215
+ validates :address, length: { maximum: 200 }
216
+
217
+
218
+
219
+ ##
220
+ # enums
221
+ ##
222
+
223
+ enum capital_unit: {
224
+ us_dollar: 1,
225
+ yen: 2,
226
+ yuan: 3
227
+ }
228
+
229
+
230
+ attribute :establishment_year
231
+ attribute :establishment_month
232
+ attribute :establishment_date
233
+
234
+ end
235
+
236
+ ```
237
+
238
+ ```model
239
+ class User < ApplicationRecord
240
+ # 論理削除使用
241
+ acts_as_paranoid
242
+
243
+ devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :lockable, :password_expirable, :secure_validatable, :password_archivable, :expirable
244
+ include DeviseTokenAuth::Concerns::User
245
+
246
+ ##
247
+ # relations
248
+ ##
249
+
250
+ belongs_to :su_account, optional: true
251
+
252
+ ##
253
+ # enums
254
+ ##
255
+
256
+ enum account_type: { DTVS: "01", DTVSCM: "02", Company: "11", CompanyPIC: "12", SUUser: "21" }, _prefix: true
257
+
258
+ ##
259
+ # validates
260
+ ##
261
+
262
+ validates :account_type,
263
+ presence: true
264
+
265
+ validates :password,
266
+ password_complexity: true,
267
+ if: :password_required?
268
+
269
+ # 姓
270
+ validates :last_name,
271
+ presence: true,
272
+ length: { maximum: 20 }
273
+
274
+ # 名
275
+ validates :first_name,
276
+ presence: true,
277
+ length: { maximum: 20 }
278
+
279
+ # メールアドレス
280
+ validates :email,
281
+ presence: true,
282
+ length: { maximum: 256 },
283
+ email: true,
284
+ uniqueness: true, on: :create
285
+
286
+
287
+ # 電話番号
288
+ validates :tel,
289
+ allow_blank: true,
290
+ length: { maximum: 25 },
291
+ format: { with: /(\d|-|+).*/ }
292
+
293
+ end
294
+
295
+ ```