Just to clarify, I think you're asking why you're not even able to set the written_on
attribute, let alone validate it-- when I used your exact code locally and tried to create a new book, on submit I got undefined method `written_on(1i)=' for Book
.
This is because the Book
model isn't inheriting from ActiveRecord::Base
; you're just including ActiveModel::Model
and ActiveModel::Validations
. The Rails guide on form helpers says "when Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type."
So I started looking through the Rails source to see where this functionality was implemented, and it's currently in ActiveRecord::AttributeAssignment. There is currently an open pull request that moves this functionality to ActiveModel so that in cases like yours, you'd be able to use it by including ActiveModel::AttributeAssignment
.
I'm not sure what you can do until that gets merged in and released. I tried including ActiveRecord::AttributeAssignment
and still got the same error, and looking at the pull request, it doesn't seem to be that straightforward. You could fork Rails and apply that pull request, but you'd have to maintain your own Rails for a while until that lands, then get back on a released version.