# Bug OP-19966 - UserWorkingHours create crashes with 500 when a weekday is nil

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-19966

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | Medium |
| Module | Modules and project features / Time tracking |
| Target versions | 17.9.0 |
| Observed in versions | 17.7.0 |
| Sprint |  |
| Backlog bucket |  |
| Bug found in version (deprecated) | 17.7.x |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers | Klaus Zanders |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### Before filing a report

Confirmed reproducible on the latest available patch release of the 17.7 minor line (17.7.1, the latest at the time of testing) via direct REST API calls against a real instance. Searched community.openproject.org for existing reports (by "UserWorkingHours nil", "working_hours weekday presence", "working hours undefined method") — found no existing report covering this. A minimal, verified fix is proposed below, also submitted as a pull request: https://github.com/opf/openproject/pull/24773

### Steps to reproduce

1. Enable the `user_working_times` feature flag and ensure the acting user has the `manage_own_working_times` permission (self-service on their own working hours).
2. Login as that user.
3. Send `POST /api/v3/users/me/working_hours` with a `validFrom` date and only SOME of the 7 weekday-hours fields set, e.g.:
   ```json
   {"validFrom":"2076-01-01","mondayHours":8.0,"tuesdayHours":8.0,"thursdayHours":8.0,"fridayHours":8.0,"saturdayHours":0,"sundayHours":0}
   ```
   (Note: `wednesdayHours` is omitted.)

### What is the buggy behavior?

The request returns **HTTP 500** instead of a clean validation error:
```json
{"_type":"Error","errorIdentifier":"urn:openproject-org:api:v3:errors:InternalServerError","message":"An internal error has occurred. undefined method '/' for nil"}
```

Root cause: `app/models/user_working_hours.rb` declares `presence`/`numericality` validations on all 7 weekday-hours columns, but the underlying DB columns have no default and stay `nil` when omitted from the request. The model dynamically defines a getter for each day that unconditionally divides the raw (possibly nil) minutes column:
```ruby
DAYS.each do |day|
  define_method("#{day}_hours") do
    (public_send(day) / 60.0).round(2)
  end
  ...
end
```
The `numericality` validator calls this getter during validation, and `nil / 60.0` raises `NoMethodError`, crashing before a clean validation error can be produced. A second, related crash site exists in the model's own custom validation:
```ruby
def at_least_one_working_day_selected
  if DAYS.all? { |day| public_send(day).zero? }
    errors.add(:days, :no_working_day)
  end
end
```
`public_send(day)` here reads the raw (possibly nil) minutes column directly, and `nil.zero?` also raises `NoMethodError`. The `presence`/`numericality` validations run before this custom validation, so on stock code, omitting even a single weekday field already crashes at the first site. This second site is a separate, latent nil-unsafe crash that becomes reachable specifically once the first getter is made nil-safe (fixing only the getter and leaving this check untouched would trade one 500 for another when every weekday is omitted at once) -- both sites need to be fixed together.

### What is the expected behavior?

The request should return a clean **HTTP 422** listing which weekday fields are missing/invalid (e.g. "Wednesday hours can't be blank.", "Wednesday hours is not a number."), and -- if every weekday is omitted -- the existing "at least one working day" validation message, instead of crashing.

### Logs

```
HTTP/1.1 500 Internal Server Error
content-type: application/hal+json; charset=utf-8
{"_type":"Error","errorIdentifier":"urn:openproject-org:api:v3:errors:InternalServerError","message":"An internal error has occurred. undefined method '/' for nil"}
```

### Screenshots and other files

None — this is a pure backend/API bug with no UI reproduction path tested; the REST API reproduction above is sufficient to demonstrate it independent of any client.

### Environment information

**OpenProject installation type**

* Docker All-in-one container (official `openproject/openproject` image: `17.7.1`)

**OpenProject version**

Reproduced on `17.7.1` (the latest release in the 17.7 line at the time of testing). Confirmed present unchanged in source on the `dev` branch (checked against the full source checkout).

**Browser**

Not applicable — reproduced via direct REST API calls, no browser involved.

**Operating System**

Not applicable — server-side bug, reproduced against a Linux-based Docker container; the client OS is irrelevant to the reproduction.

**Language**

Not applicable — no UI/browser interaction involved in the reproduction.
