TL;DR include ActiveRecord::AttributeAssignment
in your form object.
Problem
We generated a Visit
model with a started_at
date time property, using rails g scaffold Visit started_at:datetime
. The default form input for a datetime in Rails uses a datetime_select
1 which renders 5 selects - year, month, day, hours and minutes.
This works perfectly fine.
The model grew and it became clear that the Visit
model would have to be replaced with a VisitForm
object to handle relationships and additional validations.
When switching over to the VisitForm
model we instantly hit failing tests with an unknown attribute 'started_at(1i)'
error message.
When we inspected the form we could see that datetime_select
1 creates selects with the suffix of (1i)
, (2i)
, etc. for each of the selects.
Somewhere in ActiveRecord, takes these params and reconstructs a datetime value, but where?
Journey
After a lot of head-scratching and “datetime rails” searches, we re-read the datetime_select
1 documentation for the ??th time and came across the line
The selects are prepared for multi-parameter assignment to an Active Record object.
“multi-parameter assignment” is the phrase that finally caught our attention. The next Google search took us to Stack Overflow where the answer lay.
There’s every possibility that the datetime_select
will not stay in the application but this sure was a fun journey to keep it there for the time being.