ActiveRecord validation error formatter

4 views
Skip to first unread message

AndrewO

unread,
May 4, 2011, 12:37:54 PM5/4/11
to wrong
I was about to write a post asking for advice on writing a formatter
for ActiveRecord objects that would show full error messages when
valid? was false, but then I just looked at messages/array_diff.rb and
figured it out myself. Awesome.

I've posted what I came up with as a gist. I plan on packaging it once
I'm satisfied with it and have used it a bit longer. I'd be interested
in some feedback since so far I've only tested it on things like
`assert {some_record.valid?}`.

https://gist.github.com/955522

Some thoughts I had for making it more general:

* Checking that it includes ActiveModel::Errors instead of being
ActiveRecord::Base so that it'll work for other model classes.
* Removing the method check and just always displaying the full error
messages if the model is invalid. I know sometimes you want to check
that it's invalid, so that's why I didn't go this route.

In any event, this was much easier than I thought it would be and
should speed up my workflow. Thanks for this library!
Reply all
Reply to author
Forward
0 new messages