質問編集履歴
1
schema.rbとattendance.rbを追加いたしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,6 +124,94 @@
|
|
124
124
|
|
125
125
|
|
126
126
|
|
127
|
+
*schema*
|
128
|
+
|
129
|
+
```rb
|
130
|
+
|
131
|
+
class CreateAttendances < ActiveRecord::Migration[5.2]
|
132
|
+
|
133
|
+
def change
|
134
|
+
|
135
|
+
create_table :attendances do |t|
|
136
|
+
|
137
|
+
t.date :worked_on
|
138
|
+
|
139
|
+
t.datetime :started_at
|
140
|
+
|
141
|
+
t.datetime :finished_at
|
142
|
+
|
143
|
+
t.string :note
|
144
|
+
|
145
|
+
t.references :user, foreign_key: true
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
t.timestamps
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
*attendaceモデルファイル*
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
```rb
|
172
|
+
|
173
|
+
class Attendance < ApplicationRecord
|
174
|
+
|
175
|
+
belongs_to :user
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
validates :worked_on, presence: true
|
180
|
+
|
181
|
+
validates :note, length: { maximum: 50 }
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
validate :finished_at_is_invalid_without_a_started_at
|
186
|
+
|
187
|
+
validate :started_at_than_finished_at_fast_if_invalid
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
def finished_at_is_invalid_without_a_started_at
|
192
|
+
|
193
|
+
errors.add(:started_at, "が必要です") if started_at.blank? && finished_at.present?
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
def started_at_than_finished_at_fast_if_invalid
|
200
|
+
|
201
|
+
if started_at.present? && finished_at.present?
|
202
|
+
|
203
|
+
errors.add(:started_at, "より早い退勤時間は無効です") if started_at > finished_at
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
```
|
214
|
+
|
127
215
|
|
128
216
|
|
129
217
|
宜しくお願い致します。
|